Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way (or a specific shell) to navigate the history in the following way: let's say I wrote the following command two weeks ago:

scp ../../../file.txt ../../../...

I don't want to use the arrows to find the command because it will take too much time. Instead, I'd like to type scp to narrow down the possibilities and then browse the history.

share|improve this question

migrated from stackoverflow.com Jul 22 '11 at 16:14

This question came from our site for professional and enthusiast programmers.

2  
Note that this is a function of your shell (bash, tcsh, zsh, fish ...) not of "unix" in some general sense. If you don't have a clue what I'm talking the bash related answer below will probably work for you. Or try printenv SHELL. –  dmckee Jul 22 '11 at 16:21
add comment

6 Answers

up vote 4 down vote accepted

For Ctrl-r (reverse search) each time you hit Ctrl-r you go backward to the next earliest match in history so this is likely the best option.

However, there are other options as well like the ! operator. If the last command was unique (scp was not executed again since then) you can use !scp which will execute the last command that began with scp in your history. Also, if you do the history | grep scp approach you can use ! to execute the command number. For instance:

$ history | egrep scp
 268  scp foo@bar:/home/foo/file .
$ !268

That !268 will execute command 268 exactly as it appears in the history output sans the preceding number.

share|improve this answer
    
Very very useful! Many thanks. –  user247866 Jul 22 '11 at 14:34
    
Also, !scp will give you the most recent history entry beginning with "scp" –  glenn jackman Jul 22 '11 at 15:20
add comment

Use vi navigation:

set -o vi
[Esc]/scp[Enter]

Hit n to go to the next match, or N to the previous match. i to insert text, A to append

share|improve this answer
add comment

I have

"\e[B": history-search-forward
"\e[A": history-search-backward

in $HOME/.inputrc, and I believe this ties the behaviour you want to the up and arrow keys. To elaborate, 'man readline' tells us history-search-{forward,backward} will take into account the current string typed on the command line:

   history-search-forward
          Search  forward  through  the history for the string of characters between the start of the current line
          and the current cursor position (the point).  This is a non-incremental search.
   history-search-backward
          Search backward through the history for the string of characters between the start of the  current  line
          and the point.  This is a non-incremental search.
share|improve this answer
    
I've bount it to the Pg-Up and Pg-Down keys. –  ott-- May 28 '13 at 11:20
add comment

Try Ctrl+r, it should do what you need.

share|improve this answer
add comment

Try Ctrl+r to search the history. You should get a prompt like the following:

(reverse-i-search)`':
share|improve this answer
    
Exactly what I needed. Thanks! –  user247866 Jul 22 '11 at 14:35
add comment

I use

history | grep scp 

and then copy, paste and edit (if required) or just ! <command number>

share|improve this answer
    
Thanks. This is indeed simple, I should have thought about it... I'll use the Ctrl+r option, it's faster. –  user247866 Jul 22 '11 at 14:32
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.