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.

Is there a better way to search my history file for a command than grep? I do have some idea what the command starts as, but I don't know how far back in the history it is.

update: was formerly zsh specific but due to overlapping answers feel free to answer for any shell (or mode (vi/emacs)) here, just note if it is specific.

share|improve this question
add comment

12 Answers

up vote 31 down vote accepted

Ctrl+R is usually the best way, as descriptor said. You can also use !string, which runs the most recent command starting with string, or !?string?, which runs the most recent command that contains string.

(I think that's the only stuff relevant to this question, but I covered much more of the history commands in this answer)

share|improve this answer
    
how do you make the 'ctrl+r' look like that? –  xenoterracide Aug 16 '10 at 21:23
5  
@xen <kbd>Ctrl+R</kbd>. answer source –  Michael Mrozek Aug 16 '10 at 22:01
    
I didn't realize SE engine accepted any html thanks. –  xenoterracide Aug 16 '10 at 22:37
add comment

Not sure if this will help, but I like to use ctrl-r and then start typing the command. It should go through your history and present you with the first match. To toggle through the matches just hit ctrl-r again.

share|improve this answer
2  
Sorry, forgot about the zsh. You might have to add this keybinding to your ~/.zshrc: bindkey '^R' history-incremental-search-backward –  decriptor Aug 16 '10 at 19:49
add comment

There is a way to use Ctrl-R with tcsh. Add the following in your ~/.cshrc:

bindkey "^r" i-search-back (it is just a caret sign and the letter r)

That's the biggest time saver for me.

share|improve this answer
add comment

I use following two most of the time.
But please note both of them search for starting chars.

  1. tcsh: type some few charecters of the command then Esc+p it will show the previous command starting with that key sequence press it again it will show another command.
  2. ksh : use fc -e - <SPACE> <first few char for your previous > this will run last command matching that sequence.

I was not able to find something like Ctrl+R in Bash for my ksh and tcsh.

share|improve this answer
3  
Ctrl+r works a little differently in ksh than Bash. Press Ctrl+r then type the string you're looking for and press enter. To search for the same thing, press Ctrl+r and enter without having to re-type the string. This is in set -o emacs mode, by the way. –  Dennis Williamson Aug 17 '10 at 19:32
    
@Dennis: +1 :-) –  Hemant Aug 18 '10 at 5:18
add comment

The interactive prompt invoked by Ctrl-R, which had been already mentioned, is the most convenient way. Additionally it's popular to remap Up and Down arrow keys to search the history for a prefix currently on the command line, however this requires some changes to the ~/.inputrc file.

The classic and less interactive solution is to use shell history expansion. Typing

!foo

will execute the last command that starts with foo and

!?foo

will execute the last command that contains foo. More information about history expansion can be found in the Bash reference manual, assuming your shell is Bash or compatible.

share|improve this answer
add comment

When using Bash, type Control-R and then start typing. Typing Control-R repeatedly after inputting some text will move you back in matching command lines.

share|improve this answer
add comment

Yes it exists, at least if you are using zsh. Just start typing the command and then press M-p (ESC p) and it will find previous commands that matches what you typed. Press it again to find the next match.

share|improve this answer
    
This answer can do with more explanations. –  phunehehe Feb 6 '12 at 15:43
add comment

I'm surprised this hasn't been mentioned - but you can select vi-mode in bash or ksh by doing this:

set -o vi

...or using this as the contents of ~/.inputrc (for GNU readline used in programs such as bash):

set editing-mode vi

Then after that, you can use Esc to enter command mode and use k to go back in history, and j to go forward. You can also search using / to search normally, and ? to search in reverse.

If you use ~/.inputrc, then this will work not just in GNU bash but in anything that uses GNU readline such as CLISP and possibly programs like the MySQL command shell and like the CPAN shell.

share|improve this answer
add comment

If you add stty -ixon to your ~/.bashrc file then Ctrl-s will go in the opposite direction to Ctrl-r.

share|improve this answer
    
I wanted to use C-s for this purpose, but my terminal got frozen; beware! (Well, I haven't yet employed any stty ixon, not sure what this is for.) –  imz -- Ivan Zakharyaschev Apr 27 '11 at 12:26
    
@imz: C-q will "unfreeze". It and C-s are the usual software flow control characters which were once useful to manually interrupt (pause) the flow of output when slow serial connections were used. Now output is so fast in most cases that what you want to see when you press C-s has already scrolled by. stty -ixon turns off software flow control. –  Dennis Williamson Apr 28 '11 at 11:19
    
thanks for the comment! –  imz -- Ivan Zakharyaschev Apr 28 '11 at 11:54
add comment

might be zsh specific. In researching this answer I found vi-history-search-backward which allows one to search with / in vi mode (which I didn't mention I was in) you can then use n and N to search forward and backward respectively (after pressing enter).

share|improve this answer
add comment

If you add the following to ~/.inputrc

"\e[5~": history-search-backward
"\e[6~": history-search-forward

and restart Bash, you can start typing, and then use Page Up and Page Down to move backward and forward through the history of lines starting with the same prefix you just typed.

(Some may prefer to use \e[A and \e[B, which will replace the normal Up and Down actions instead of binding new keys.)

share|improve this answer
add comment

If you type !ls then bash will look through your history and execute the most recent command that begins with "ls"

share|improve this answer
add comment

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.