Revert to the original command when you've have made multiple changes to the line:
Undo all changes made to this line. This is like
executing the undo command enough times to get back to the beginning.
M is the meta key, which is altr for me.
Gratuitous, Yet Helpful Info
When you have executed a command like OP had previously done (it happens), there is nothing to "undo" because the commands are handled and remembered by the GNU Readline library and are not written to
$HISTFILE until the shell exits. The reason I mention this is because you can't just
$HISTFILE like one might expect and it may not be ideal to exit the shell.
So here are a couple of options to save you from scrolling back through all previous readline commands.
If you remember some of the command, press ctrl+r and type what you remember to search through previous commands containing that string. Pressing ctrl+r again will display the next most recent, searching backwards.
For example, type
ls -l then ctrl+r as many times as it takes to find the previous command you seek. If you scroll past it, ctrl+s will search forward from the current position.
fc bash builtin command is helpful to list the index numbers alongside previous readline commands.
fc -l -100 will list the previous 100 commands in readline.
Additionally, if OP knows he's looking for a previous
ls -l command, he could pipe the output to
grep such as:
fc -l -100 | grep 'ls -l'
This should output a list of previous
ls -l commands preceded by an index number. The output looks like this:
2065 ls -l
Now you can use the event designator
n is the index number. In this example, executing
!2065 will expand to
Although it doesn't help after the fact, anyone looking to preserve the recent command history when modifying previous commands should see the
HISTORY EXPANSION section of
man bash. Here are some alternative methods for modifying old commands without overwriting the history.
Event Designators section shows how you can easily edit string from the most recently used command.
Quick substitution. Repeat the previous command, replacing string1 with string2. Equivalent to
``!!:s/string1/string2/'' (see Modifiers below).
$ echo foo
The most recent commands will now show:
The above example also explains how to use
sed to replace a string in an event designator.
!-n Refer to the current command minus n.
So if 3 commands up in the history is
echo foo then you would use:
Note that event designators will appear in your history as the command executed, in this case
echo bar. It will not appear in the history as
Just throwing that out there as it seems closely related, even if it is more of a "don't do" than the "undo" solution OP is looking for.