Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, I've looked at history and at Ctrl+R, but they are not what I thought I knew. Is there a way that I can type in the beginning of a command, and cycle through the matches in my history with some bash shortcut?

# mysq(some shortcut key)

Gives me:

# mysqldump  --add-drop-table -e -q -n -C -u 
(some shortcut key)
#  mysql -u ben.dauphinee -p
share|improve this question
up vote 39 down vote accepted

Pressing Ctrl+R will open the reverse history search. Now start typing your command, this will give the first match. By pressing Ctrl+R again (and again) you can cycle through the history.


Would give:

mysqldump  --add-drop-table -e -q -n -C -u 

Ctrl+R again:

mysql -u ben.dauphinee -p
share|improve this answer
Thanks. Also, Esc will put the selected command on your command line without executing, while Enter will run the selected command. – Ben Dauphinee Jan 3 '11 at 16:08
I find CTRL+E better for this than ESC, because escape is awkward to reach for but I am already hitting CTRL+R, so E is right there. This also positions the cursor at the end of the line, which is handy in case I wanted to change something (like a file name) which I find to be a common case. – Sorpigal Jan 3 '11 at 17:41

To expand on what Gilles said, I have the following in my .inputrc to bind the up/down arrow key to history-search-backward and history-search-forward:

# Key bindings, up/down arrow searches through history
"\e[A": history-search-backward
"\e[B": history-search-forward
"\eOA": history-search-backward
"\eOB": history-search-forward

Just type something (optional), then press up/down arrow key to search through history for commands that begin with what you typed.

To do this in .bashrc rather than .inputrc, you can use:

bind '"\e[A": history-search-backward'
share|improve this answer
those binding belong into ~/.inputrc ...thanks for supporting my lazyness. – syslogic May 20 '15 at 17:03
It is worth noting that the '\e' is crucial, at least for bash debutants. Just typing Ctrl-V to get the keystroke for your command doesn't tell the whole story. For example, typing Ctrl-V Meta-P (i.e.) Alt-P) returns ^[p -- to get the behavior you want, replace the "^[" with '\e'. – MrMas Jun 2 '15 at 18:04
Agree with @syslogic -- put the lines in .inputrc and drop the bind and single quotes. – MrMas Jun 2 '15 at 18:04
Thank you. I've updated the answer to use .inputrc. – raychi Jun 4 '15 at 0:07
See superuser.com/questions/241187/how-do-i-reload-inputrc. You can't source the file. – raychi Feb 22 at 23:47

You can use the readline commands history-search-backward and history-search-forward to navigate between commands lines beginning with the prefix you've already typed. Neither of these commands are bound to keys in the default configuration.

Zsh (zle) has similar commands history-beginning-search-backward and history-beginning-search-forward, also not bound to keys by default. There are also history-search-backward and history-search-forward, which uses the first word of the current command as the prefix to search regardless of the cursor position.

share|improve this answer

If you want a good overview of very similar commands you can get a list with:

history|grep KEYSTRING

Then copy and paste with the middle button of your mouse.

Rgds - Joe

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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