Bash does have that feature too, but it's not enabled by default. You can bind it to cursor up/down by sticking this into ~/.inputrc
:
"\e[A": history-search-backward
"\e[B": history-search-forward
I prefer to bind it to Ctrl+up/down instead:
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward
edit: To preserve ctrl+left and ctrl+right for moving back and forward in whole words, also include these lines in ~/.inputrc
file:
# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word
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
Press Ctrl+R and type
ssh
. Ctrl+R will start search from most recent command to old one (reverse-search). If you have more than one command which starts withssh
, Press Ctrl+R again and again until you find the match.Once you've found the match you can press Enter to execute the command or left / right cursor to just select the text of the command.
There is no default reverse option for Ctrl+R to invert the direction of the search but here you will find some suggestions about it.