I have searched, but not found anything on this. I am looking for a functionality in bash, using a terminal.

Way back when, I had a user on a debian system, and a friend set me up with a convenient history search feature (I believe I used tcsh then), where I would type the beginning of a previous command, hit up-arrow, and it would do a search, based on the partial string.

E.g. if my history is:

./script.pl arg1
cat output
cat output | grep yada

And I type ., and press up-arrow, it would show me: ./script.pl arg1. Press it again and it would show ./script.pl, etc.

Very much like it would perform a grep on .bash_history. Is there a way to get this functionality?


Open your ~/.inputrc and add these lines:

## arrow up
## arrow down

TIP: If you don't have this file, read at the bottom how to create it without loosing the default bindings.

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:

  1. Start cat in a terminal (just cat, no further arguments).
  2. Type keys on keyboard, you will get things like ^[[A for up arrow and ^[[B for down arrow.
  3. 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
  • 4
    Great, but this solution disables the ctrl-left/ctrl-right shortcuts. – Eyal Feb 2 '16 at 14:22
  • Can anyone tell me how can I achieve the same for make – Hitesh Kumar Feb 10 '17 at 17:29
  • @lesmana, i would give extra points for the cat part alone – Mike D Nov 17 '17 at 16:50
  • Seconding @Eyal, anyone have advice on how I can have this but preserve ctrl-left/ctrl-right shortcuts? – kbrose Aug 28 '18 at 21:59
  • 10
    To preserve all defaults add $include /etc/inputrc, preferably on the first line. – Tulio Casagrande Sep 5 '18 at 17:38

Create a file named setup_readline.sh with mode 644 in /etc/profile.d/ with following content, login and check you preferred keys:

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

I think is the best way to do this. Mostly if you using configuration management systems such as chef, puppet, etc

And system config still untouched!

  • 5
    Oddly the ~/inputrc didn't work for me - but putting these commands into .bash_profile did. – Joe Jun 7 '15 at 18:45
  • 1
    .inputrc doesn't exist in my home folder and I prefer to have these kind of settings in .bashrc anyway so I can just carry around one file for my settings. So this is probably a better answer then accepted one. – Shital Shah Jun 4 '16 at 0:33
  • .inputrc works fabulously for me, but not after I load screen. The odd thing is, other .inputrc shortcuts work, but not these. So I use both solutions now. – Orwellophile Nov 28 '16 at 4:25
  • This is super-helpful when, for example, you log into a server to do things and are missing the heck out of these and don't want to edit and reload the ~/.inputrc on the server. – danmactough Dec 7 '17 at 20:56

Ctrl + R will give you this sort of functionality. Start typing a partial command and it will come up with old ones which you can navigate with the up and down arrows.

More info here.

  • 2
    Thanks for your reply. Yes, I have tried that, but I find it a bit... finicky. It will not allow me to scroll through commands beginning with the partial, it only shows the first. It also searches the entire command for the character, which is sort of useless. – TLP Sep 3 '11 at 12:23
  • 5
    @TLP, repeat hitting Ctrl+r – akostadinov Oct 3 '14 at 21:54

To complete lesmana's answer regarding ~/.inputrc that I should create under my Trusty 14.04, verify/add in ~/.bashrc (the first line already exists in my ~/.bashrc):

shopt -s histappend  
PROMPT_COMMAND='history -a'  

It's already explained in French here.

  • 3
    This command basically instructs bash to immediately append command to history file instead of waiting for session to end. Its useful if you have lot of terminals open and don't want to loose history in one but otherwise not necessary. digitalocean.com/community/tutorials/… – Shital Shah Jun 4 '16 at 0:30

Perhaps you may want to try https://github.com/dvorka/hstr which provides simple and multi-line simple filtering of Bash history which as based on a metrics (when, how often and length of commands matters) - you can use arrow keys to easily navigate the history:

enter image description here

It can be easily bound to Ctrl-r and/or Ctrl-s


The following solution combines & extends some of the above mentioned approaches: add the following lines to your ~/.bash_aliases file (no need for ~/.inputrc) in order to achieve the following behavior:

  1. move up/down in history (only entries with same prefix) by pressing CTRL+UP and CTRL+DOWN. This can co-exist to the standard functionality of moving up/down in the full history via UP and DOWN alone
  2. jump to next/previous word in a commandline by pressing CTRL+RIGHT and CTRL+LEFT
bind '"\e[1;5A":history-search-backward'
bind '"\e[1;5B":history-search-forward'
bind '"\e[1;5C":forward-word'
bind '"\e[1;5D":backward-word'

Re-login to the shell (or run source ./bash_aliases) in order to apply the changes.

  • Hi, do you know where I can find out about how to create more complicated keyboard shortcuts? Perhaps what this notation is called etc? – Ekanshdeep Gupta May 11 '20 at 10:30
  • 1
    Related terms are "keycode" or "scancode" (I am no expert). However, pragmatic solution: Running showkey -a gives you helpful output, e.g., "^[[1;5A" which means "\e[1;5A", that is, the leading "^[" seems to refer to "\e". – user27164 May 11 '20 at 18:26

There is an alternative similar to what @lesmana mentioned above, but you do not have to create a new .inputrc file.

Instead, if you have sudo permissions, you can enable this in the /etc/inputrc file. In this file are various keyboard settings including the history search feature (for 18.04 at least). The excerpt from /etc/inputrc is:

# alternate mappings for "page up" and "page down" to search the history
# "\e[5~": history-search-backward
# "\e[6~": history-search-forward

Uncomment the bottom two lines using a sudo file editor (e.g., $ sudo vim), and a new terminal session will have the history search feature (for all users). This way, you do not have to worry about creating a ~/.inputrc file, nor will you have to add an $include statement as @Tulio Casagrande mentioned in the comments to preserve your ctrl-arrow inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.