Personally, I use an alias derived from ksh:
alias r="fc -e -"
('r' for 'reerun', I suppose ...)
I can restart the last command starting with 'foo':
r foo
I can reprogram commands 46 to 50:
r 46 50
I can re-execute command 585:
r 585
The only thing you got is if you typed:
$ cd somewhere
then running 'r cd' will not work, because there was extra space in front of the 'cd' command. The fc command is at the heart of the story, and you can edit commands 46 through 50 before restarting them by typing:
fc 46 50
etc. for other options.
("-e -" in the alias means "edit with a null editor", you can write "fc -e vim" for editing with vim, but most people install VISUAL, EDITOR or FCEDIT or all three to do what is not needed .)
Otherwise, being the person "vim" (fanatic?), I use "set -o vim" and then the search tool: ESC and
/grep\ -e\ 'whatever
to search history for a command containing "grep -e" regardless. I can repeat the search, moving further back in history or in the opposite direction after overshoot or ... I assume that emacs mode has an equivalent search mechanism.
source share