How to configure up arrow to search the history? [duplicate]
Both methods below are almost equivalent, it just depends on which file you want to edit. I'd recommend .bashrc
myself, as it doesn't involve editing a local copy of a system file.
If you experience any problems with this, please comment below so that it can be fixed.
Using ~/.bashrc
-
Edit
~/.bashrc
with this command:gedit ~/.bashrc
-
Add the following lines:
bind '"\e[A": history-search-backward' bind '"\e[B": history-search-forward'
- Save then close the file.
-
Execute this command in a terminal:
source ~/.bashrc
Using ~/.inputrc
-
Execute this command in a terminal:
cp /etc/inputrc ~/.inputrc
-
Edit the new
~/.inputrc
file with this command:gedit ~/.inputrc
-
Append these lines to the file:
"\e[A": history-search-backward "\e[B": history-search-forward
References:
- incremental history searching with .inputrc ?
- In bash, how do I bind a function key to a command?
As alternative, you can press Ctrl+R then start typing the command or any part of the command that you are looking for. You'll see an auto-complete of a past command at your prompt. If you keep typing, more specific options will appear. You can also press Ctrl+R again as many times as you want to, this goes back in your history to the previous matching command each time (source: Navigating Bash History with Ctrl+R).
Another suggestion, if you want to find, for example, the last 5 commands from your history starting with "p", you can use the following command:
grep "^p" ~/.bash_history | tail -n 5