Get back history commands and arguments
It's possible to selectively access previous commands and arguments using the !
operator. It's very useful when you are working with long paths.
You can check your last commands with history
.
You can use previous commands with !<n>
being n
the index of the command in history
, negative numbers count backwards from the last command in history.
ls -l foo bar
touch foo bar
!-2
You can use previous arguments with !:<n>
, zero is the command, >= 1 are the arguments.
ls -l foo
touch !:2
cp !:1 bar
And you can combine both with !<n>:<m>
touch foo bar
ls -l !:1 !:2
rm !-2:1 !-2:2
!-2
You can also use argument ranges !<n>:<x>-<y>
touch boo far
ls -l !:1-2
Other !
special modifiers are: