10

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.

0

4 Answers 4

13

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.

3
  • 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
    Apr 30, 2013 at 14:01
  • I think that it doesn't matter at all, but just in case, I'm using the konsole terminal emulator.
    – Sekhemty
    Apr 30, 2013 at 14:03
  • Sorry, I've updated my answer about how to find previous matches. May 2, 2013 at 7:28
5

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.

1
  • 3
    An additional trick: Use !n:p. This prints the old command without executing it and puts it one back in your history. Then go to it with a single stroke of up-arrow and now you can edit it. There's lots of other silly tricks like this: ss64.com/bash/history.html Apr 30, 2013 at 15:38
3

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

0
2

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.

Not the answer you're looking for? Browse other questions tagged .