Bash history re-runs: possible command to avoid using !bang!? Bash history re-runs: possible command to avoid using !bang!? bash bash

Bash history re-runs: possible command to avoid using !bang!?


Press ^r (that's CTLR-r), type foocommand press Enter ;)That will search history and show the latest matching entry


Did you try '!f'? 'f' as first letter of the foocommand.


Personally, I use an alias derived from ksh:

alias r="fc -e -"

('r' for 'rerun', I suppose...)

I can rerun the last command starting with 'foo':

r foo

I can rerun commands 46 to 50:

r 46 50

I can rerun just command 585:

r 585

The only gotcha is that if you typed:

$  cd somewhere

then running 'r cd' won't work because there was an extra space in front of the 'cd' command. The 'fc' command underlies history, and you can edit commands 46 to 50 before rerunning them by typing:

fc 46 50

and so on for the other variants.

(The '-e -' in the alias means 'edit with the null editor'; you could write 'fc -e vim' to edit with vim, but most people set VISUAL or EDITOR or FCEDIT or all three to make that unnecessary.)

For the rest, being a 'vim' person (fanatic?), I use 'set -o vim' and then the search facility: ESC and

/grep\ -e\ 'whatever

to search history for a command containing "grep -e 'whatever". I can repeat the search, moving ever further backwards in history, or reverse direction after overshooting, or ... I assume that the emacs mode has an equivalent search mechanism.