Pragmatism in the real world

Context specific history at the bash prompt

One change I made recently to my .profile is this:

# up & down map to history search once a command has been started.
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

These two bind command change the way that the up and down arrow keys work once you start typing a command to only search the history for lines that start with what you’ve typed so far.

This means that I type, say, git and then press ↑ & ↓ to go through all the times I’ve typed a git command without having to go through all the other commands.

It’s quite handy and I find it easier to use than ctrl+r.

5 thoughts on “Context specific history at the bash prompt

  1. Awesome. But bind doesn't work in zsh I found. zsh users can use:

    bindkey "^[[A" history-search-backward
    bindkey "^[[B" history-search-forward
    

    to achieve the same.

  2. You can also do this with a .inputrc file, along with making it case insensitive and provide a list the possible completion values. The I got this from a post by Jude Robinson here on coderwall.com

  3. It's worth checking /etc/inputrc on your system, as there are usually suggestions in there that can make working in the terminal more pleasant.

    I like using:
    "\e[5~": history-search-backward
    "\e[6~": history-search-forward

    which make Page Up and Page Down search through the history, instead of ↑ and ↓ as you describe. These are enabled by default nowadays on Fedora (see https://bugzilla.redhat.com/show_bug.cgi?id=500989).
     

Comments are closed.