There are times when I want to take the previous command's parameters and apply them to the current command.

Here is an example

$ cat foo.txt
$ vim foo.txt

I'd like to find a shortcut to not have to type out vim foo.txt

  • 3
    Those aren't arguments, but the output, that you're looking for, since ~/foo.txt is printed by locate. – user1686 Jul 17 '13 at 18:18
  • @grawity you are completely right. I modified the question because it made no sense the way I asked it. – spuder Jul 17 '13 at 18:27

I often use the Bash command yank-last-arg, shortcut M-. or Esc+.. This copies the last argument from the previous history entry into the current line at the current position, so you can check and edit the argument.

  • Plus 1 for esc . – spuder Jul 17 '13 at 18:38

The solution is to use !$


vim !$

To grab the last parameter in the last command, use `!foo:$ as shown below

cp foo bar
!cp:$ foobar
#This will copy foo to bar, and then bar to foobar

You could alternativly use the following syntax

cp foo bar


  • 3
    Be aware, !$ only expands to the last argument, not all. And this does not answer your question as stated—do you want command substitution as in vim "$(locate foo.txt)"? – slhck Jul 17 '13 at 18:19
  • Thanks for pointing that out. Since there are no answers yet, I am going to clarify the question. – spuder Jul 17 '13 at 18:25
  • 2
    To take all the parameters, use !* -- gnu.org/software/bash/manual/bashref.html#History-Interaction – glenn jackman Jul 17 '13 at 18:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.