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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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'
Method 3
You could also press the PAGEUP button to auto-complete a command. It basically searches the bash_history file.
Method 4
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.
Method 5
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0