There are many ways to skin this cat!
To replace a specific string, ^old^new^extra
will take your previous command, replace old
with new
, and append extra
. You may stop as early as you like; e.g. ^old
will replace old
with the empty string and append the empty string (essentially deleting old
and doing nothing more).
1. ^diff^checkout
2. ^cat^rm
Especially useful variant of this:
for i in `seq 1 10`; do echo something complicated; done
# inspect the output to see if it looks right
^echo
To replace a specific word, you can use !m:n-p
to refer to words n
through p
of the command m
(use negative numbers to count back from the current command). You can omit the -p
part to refer to a single word, and omit the :m-p
part to refer to an entire command. Special forms !!
for !-1
, !$
for the last word of the previous command, and !*
for the arguments (all but word 0
) of the previous command are pretty handy.
1. !!:0 checkout !!:2-
2. rm !*
I often use both of these; zsh is especially nice here, as hitting tab will expand the cryptic stuff out to make sure you got it right.
There are also so many ways to do this with line editing. Read up on your shell's keybindings.