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.

  • 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 --- ex-moderator kitten Jul 22 '11 at 16:21

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.

  • Also, !scp will give you the most recent history entry beginning with "scp" – glenn jackman Jul 22 '11 at 15:20

I use

history | grep scp 

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

  • 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

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


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


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:

          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.
          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.
  • I've bount it to the Pg-Up and Pg-Down keys. – ott-- May 28 '13 at 11:20

Use vi navigation:

set -o vi

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.