120

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
Chris Down
  • 125,559
  • 25
  • 270
  • 266

5 Answers5

123

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.

mysq(Ctrl+R)

Would give:

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

Ctrl+R again:

mysql -u ben.dauphinee -p
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
wag
  • 35,944
  • 12
  • 67
  • 51
  • 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 03 '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 03 '11 at 17:41
  • Sadly, it doesn't work on Cygwin – ATorras Feb 19 '18 at 08:54
  • 7
    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 09 '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 06:16
101

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'
raychi
  • 1,191
  • 1
  • 8
  • 4
  • 7
    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 02 '15 at 18:04
  • Agree with @syslogic -- put the lines in .inputrc and drop the bind and single quotes. – MrMas Jun 02 '15 at 18:04
  • copy-pasting this into .inputrc on osx, i get : source ~/.inputrc -bash: \e[A:: command not found -bash: \e[B:: command not found -bash: \eOA:: command not found -bash: \eOB:: command not found – Walrus the Cat Jul 03 '15 at 21:50
  • 1
    See http://superuser.com/questions/241187/how-do-i-reload-inputrc. You can't source the file. – raychi Feb 22 '16 at 23:47
  • 3
    To get this to work in your current shell, run bind -f ~/.inputrc – Roman Oct 17 '18 at 21:05
  • I think this is the nicer solution. Thank you @raychi – MkMan Aug 13 '20 at 22:55
10

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

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

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.

7

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