Make sure all non-printable bytes in your PS1 are contained within \[ \]
. Otherwise, bash will count them in the length of the prompt. It uses the length of the prompt to determine when to wrap the line.
For example, here bash counts the prompt as 19 columns wide, while the prompt displayed by the terminal is only 10 columns wide (My prompt
written in cyan, and >
written in default color):
PS1='\e[36mMy prompt\e[0m>' # bash count: 19, actual: 10
while here it only counts the prompt as 10 columns wide because it ignores the bytes between the special \[
and \]
escapes:
PS1='\[\e[36m\]My prompt\[\e[0m\]>' # bash count: 10, actual: 10
For good practice though, use tput
to generate the terminal escapes rather than hard coding them:
cyan=$(tput setaf 6) # \e[36m
reset=$(tput sgr0) # \e[0m
PS1='\[$cyan\]My prompt\[$reset\]>'
See http://mywiki.wooledge.org/BashFAQ/053, and also http://wiki.bash-hackers.org/scripting/terminalcodes for more on tput
.
Open your ~/.inputrc
. If you don't have this file, see the end for how to create it. Add these lines:
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
Lines starting with #
are comments. I can't remember what is backward and what forward. Experiment with it. Maybe you have to switch backward and forward.
Just re-open possibly open terminal windows for the new behaviour to become effective.
A bit background information:
Bash is using readline to handle the prompt. ~/.inputrc
is the configuration file for readline. Note that this will also take effect in other software using the readline library, for example IPython.
Read the bash manual for more information about readline. There you can also find more history related readline commands.
To get the escape codes for the arrow keys you can do the following:
- Start
cat
in a terminal (just cat
, no further arguments).
- Type keys on keyboard, you will get things like
^[[A
for up arrow and ^[[B
for down arrow.
- Replace
^[
with \e
.
For more information about ^[
and \e
see here: https://unix.stackexchange.com/a/89817/380515
If you don't already have a ~/.inputrc
file, copy the default settings over, or all the other default key bindings will be overridden:
cp /etc/inputrc ~/.inputrc
or begin your ~/.inputrc
file with the following line
$include /etc/inputrc
[1]:
Best Answer
The variable
$_
is used to substitute the most recent parameter.So, in the example you mentioned, you'd do something like:
cd $_
will change directory to the most recent parameter i.e.~/folder
.For more such variables, have a look at https://stackoverflow.com/a/5163260/1626345.