Search Shell History Using Arrow Keys

FreeBSD's root user can easily search its shell command history using the arrow keys. Add the same search capability in the Bash shell with this change.

By default, the FreeBSD root user can type part of a command and then search through history for previous commands beginning with those characters using the up/down arrow keys. If you're new to FreeBSD or csh, you might think the convenient shell history search is specific to root or csh/tcsh. Fortunately, it's not. You can get similar functionality in Bash by creating or editing ~/.inputrc to add the following lines:

# Allow history searching with the up/down arrows:
"\e[A": history-search-backward
"\e[B": history-search-forward

The syntax is a little different if you're using ~/.profile instead, which works when you only access the system over SSH. Note the single quotes around the bind parameter.

# Allow history searching with the up/down arrows:
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

You'll need to start a new terminal session for the change to take effect. This feature is a must-have once you grow accustomed to it, so it's one of the first changes I make on a new system whether it's FreeBSD, Solaris, or Linux.

, , ,
comments powered by Disqus