How can I recall a numbered history command for edit?

I've since adopted another approach to this - using ![line-number]:p

This prints the statement and adds it to history but doesn't actually execute it. I then do up arrow and change it as desired.

I combine this with my hg alias (alias hg='history | grep ') to recall history commands based on some text.

Example:

$ hg checkout

17140   git checkout README.rdoc
17143   git checkout master
17201   git checkout README.rdoc
17204   git checkout master
17923   git checkout .bashrc
18151   git checkout v311

I use this in addition to ctrl-r (reverse history search) because sometimes I prefer to see an immediate list of all the possibilities for a given string rather than just the output on 1 line that ctrl-r shows. After hg [string] I would then do ![line-number]search_string as in the hg checkout shown above.


If you set the histverify option, e.g.

shopt -s histverify

then all history substitutions are brought up for editing instead of being executed immediately. You would then need to press Enter twice instead of once after typing !755 to execute the command.

You can push arbitrary text onto the history list with history -s. Combine this with fc -nl to list a specific history entry.

history -s "$(fc -nl 755 755)"

then press Up to recall what is now the latest history entry.


You can search back through the history using Ctrl+R. If the history entry is long use the mouse (not the keyboard, that stops the search) to copy and paste part of the command to edit back in.

As @rijsg commented, you can then use the (left and right) arrows or equivalent keys to stop the search and start editing.