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
  • Side note: There is '<kbd>Alt+Tab</kbd>' for tty's without desktop. – neverMind9 May 3 '18 at 15:45

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
  • 8
    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
  • 3
    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
  • Sadly, it doesn't work on Cygwin – ATorras Feb 19 '18 at 8:54
  • 4
    On mac, at least, it seems mysq(CTRL+R)(CTRL+R) doesn't cycle through commands beginning with "mysq". You need to do (CTRL+R)mysq(CTRL+R) instead – BlueRaja - Danny Pflughoeft May 9 '18 at 13:24
  • @BlueRaja-DannyPflughoeft I have Bash setup on Windows 10 and that's how it works there as well. Took me a second to figure out how it was working. – ohsully Oct 31 '19 at 6:16

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'
  • 5
    those binding belong into ~/.inputrc ...thanks for supporting my lazyness. – Martin Zeitler 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
  • 1
    See superuser.com/questions/241187/how-do-i-reload-inputrc. You can't source the file. – raychi Feb 22 '16 at 23:47
  • 2
    To get this to work in your current shell, run bind -f ~/.inputrc – Roman Oct 17 '18 at 21:05

You could also press the PAGEUP button to auto-complete a command. It basically searches the bash_history file.

  • 1
    You have to uncomment two lines in /etc/inputrc or add these two lines to your $HOME/.inputrc file: "\e[5~": history-search-backward "\e[6~": history-search-forward – garlix Oct 18 '18 at 13:46

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.


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

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.