In reverse-i-search (Ctrl+R ) ,Any method to switch between similar commands in history

LinuxBashCommand Line

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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.

Solution 3 - Linux

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

Solution 4 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArunView Question on Stackoverflow
Solution 1 - LinuxjgrView Answer on Stackoverflow
Solution 2 - LinuxBonlenfumView Answer on Stackoverflow
Solution 3 - LinuxGiorgosKView Answer on Stackoverflow
Solution 4 - LinuxrkachachView Answer on Stackoverflow