51

I think an example will be good to understand my question.

...
scp file1 user10@192.168.10.1:/home/user1/linuxfiles/samplecode/important
...
...
scp file1 user10@192.168.10.1:/home/user1/linuxfiles/samplecode/important/tested
...
...

Assume that is the order of commands in history. If I am doing Ctrl+R and type scp it will show the last executed scp command ( ie the line ending with 'tested') .

But I want to locate the scp command ending with 'important'. So is there any way in this reverse-i-search to view all commands starting with scp, to choose the appropriate one?

dwalter
  • 7,258
  • 1
  • 32
  • 34
Arun
  • 1,651
  • 4
  • 20
  • 31

4 Answers4

105

Keep pressing Ctrl-R and it will traverse your history.

dwalter
  • 7,258
  • 1
  • 32
  • 34
jgr
  • 3,914
  • 2
  • 24
  • 28
  • 3
    Thanks, is there a possibility to use Ctrl-R backwards? So if I "ctrl-R" for some time, I see the results, but I would like to go it backwards. "Shift-Ctrl-R" does not work. EDIT: The solution is on SO, see "related". – Timo Sep 10 '14 at 06:08
  • 2
    Forward search: Ctrl-S. See http://stackoverflow.com/questions/17760474/reverse-intelligent-search-reverse-i-search-how-to-get-previous-result?rq=1 – Stefan Profanter Aug 26 '15 at 12:25
17

If your search terms are a bit more complicated/ not contiguous, another option is to grep among the history results, e.g.:

history 300 | grep scp | grep important$

This will return a list of commands in your history that match, such as:

3323  scp file1 user10@192.168.10.1:/home/user1/linuxfiles/samplecode/important
3325  scp file1 user10@192.168.10.1:/home/user1/winfiles/samplecode/important

And you can then execute the relevant command with !3325.

I sometimes find this useful when running a lot of similar commands and may have to press Ctrl+R many times to get back to the exact command.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
4

There is great alternative to Ctrl+R

install https://github.com/dvorka/hstr

run it with hh

it shows a list of you all the last commands executed (contents of ./bash_history)

if you start typing the list will be filtered based on what you type

you can use Up/Down arrows to select the desired command and select it with Enter

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
GiorgosK
  • 7,647
  • 2
  • 29
  • 26
1

I found this great tool mcfly as a replacement to the traditional Ctrl+R and it works really well. Basically you can see ALL the results as you type and you can select the command you are looking for. You can also customize the layout etc. I really recommend it.

https://github.com/cantino/mcfly

Example of mcfly session

rkachach
  • 16,517
  • 6
  • 42
  • 66