How to selectively navigate through Bash command history on a Linux terminal

5,090

Solution 1

You can search the history using Ctrl+R and then type the search string (e.g. iw to find iwconfig). Then you can then still navigate through the history at that point with the up and down arrow keys, or press Ctrl+R again to find the previous occurence.

Solution 2

After a bit of practice, I found how to use the workaround solution.

I matched the correct syntax to print a filtered list, I did it with history | grep iwconfig (it wasn't so difficult after all); with the output I can use !n with the now easy-to-read filtered list.

Solution 3

I can't comment on Stefan's answer, but you normally have to keep pressing Ctrl+R. See this page for more information.

Solution 4

Press "ctrl + r" then enter search string.

If not the one you are looking for Then again "ctrl + r"

If found, press Enter to execute it Or press right arrow -> key to edit it before using it.

Share:
5,090

Related videos on Youtube

Sekhemty
Author by

Sekhemty

_

Updated on September 18, 2022

Comments

  • Sekhemty
    Sekhemty over 1 year

    Everyone knows (or should know!) that on a terminal the up and down arrow keys are used to navigate through the history of used commands.

    When you use the terminal on a daily basis, your command history will likely be a long list of many different commands, thus causing navigation less easy. Just as an example, let's say that a month ago I used the iwconfig with a determined set of parameters (like I said, it's just an example, I don't need to mess with it right now), how can I recall that particular command with all its arguments and parameters without having to navigate back through hundreds of history commands?

    I know that I could use history to have a list of previously used commands and !n to re-execute the n command on the said list, but when the history grows large, it is not very practical too.

    The ideal thing would be to have something that will let me use some hotkey to navigate through history but just between the instances that contain what I have already typed on the command line; so in my example, after having typed iwconfig, use some keys (maybe PgUp and PgDown?) to navigate just through the entries that begins with iwconfig.

    If it is not possible, it would also be nice to print a list like history do but that will only contain what I've already typed (I suspect that history and grep command are involved but I'm not sure about the correct syntax); but the previous hotkey solution would be the best option.

  • Sekhemty
    Sekhemty about 11 years
    I'm not sure to have understood well hot it works. If I press Ctrl+R and type iwconfig, it shows me an instance that contains it (not necessarily the most recent), but if I use the arrow keys, it navigate through all items, not just the ones that contains the string. It seems to me that it select a specific point in the history to begin navigation at (instead of beginning from the bottom like the up key do), but apart from that, the navigation is the same as always. Did I miss something?
  • Sekhemty
    Sekhemty about 11 years
    I think that it doesn't matter at all, but just in case, I'm using the konsole terminal emulator.
  • Stefan Seidel
    Stefan Seidel about 11 years
    Sorry, I've updated my answer about how to find previous matches.