Repeat previous command with different arguments

If you want to re-run the command with the same arguments, you can do something like this:

vim long_filename
cat !$                     #same as 'cat long_filename'

This saves the need to re-enter the previous argument when it is passed in cat.

However, how do I pass arguments that are not the same in the last run of script / command?

long_annoying_script_name arg1 arg2
? arg3 arg4                                  #? signifies shortcut symbols such as '!$'

Of course, I could just press the up arrow and delete the arguments and enter new ones, but is there a shorter / faster way?

I do NOT want to assign an alias.

+8
source share
2 answers

!:0gotta do the trick. From the zsh documentation:

   Word designators
       A word designator indicates which word or words of a given command line
       are to be included in a history reference.  A `:' usually separates the
       event specification from the word designator.  It may be  omitted  only
       if  the  word designator begins with a `^', `$', `*', `-' or `%'.  Word
       designators include:

       0      The first input word (command).
       n      The nth argument.
       ^      The first argument.  That is, 1.
       $      The last argument.
       %      The word matched by (the most recent) ?str search.
       x-y    A range of words; x defaults to 0.
       *      All the arguments, or a null value if there are none.
       x*     Abbreviates `x-$'.
       x-     Like `x*' but omitting word $.

( bash.) !-1, , .

+8

TL; DR

Alt + 0 + .:


Ubuntu 18.04 (.. Emacs)


:

mv foo bar

up, Ctrl + w: = mv foo

Alt + 0 + .: = mv

:

  • Alt + .: (, )
  • Alt + # + .: #nth (, )
  • Alt + -, #, Alt + ., zsh: Alt + - + # + .: # (, )
  • Ctrl + w:
  • Alt + d:
  • Ctrl + k:
  • Ctrl + u, zsh: Alt + w:
  • zsh: Ctrl + u: ( bash Ctrl + u, Ctrl + k)
  • Ctrl + y: , Ctrl + u Ctrl + k
  • Ctrl + _: ( Ctrl + w)
  • Ctrl + left:
  • Ctrl + right:
  • home Ctrl + a:
  • end Ctrl + e:

  • bash: bind -lp
  • zsh: bindkey -L

,

"words" a-zA-Z a-zA-Z, .

, URL, Ctrl + w, .

: curl -I --header "Connection: Keep-Alive" https://stackoverflow.com/questions/38176514/re-run-previous-command-with-different-arguments

URL, Ctrl + w, 12 .


,

+2

All Articles