Take the 2-minute tour ×
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.

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

4 Answers 4

up vote 20 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.

mysq(Ctrl+R)

Would give:

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

Ctrl+R again:

mysql -u ben.dauphinee -p
share|improve this answer
2  
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
1  
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 .bashrc to bind the up/down arrow key to history-search-backward and history-search-forward:

# Key bindings, up/down arrow searches through history
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\eOA": history-search-backward'
bind '"\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.

share|improve this answer

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

 
discard

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.