1

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

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

2 Answers 2

4

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.

1
  • Plus 1 for esc .
    – spuder
    Commented Jul 17, 2013 at 18:38
2

The solution is to use !$

example:

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
!!:$

http://www.thegeekstuff.com/2011/08/bash-history-expansion/

3
  • 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
    Commented Jul 17, 2013 at 18:19
  • Thanks for pointing that out. Since there are no answers yet, I am going to clarify the question.
    – spuder
    Commented Jul 17, 2013 at 18:25
  • 3
    To take all the parameters, use !* -- gnu.org/software/bash/manual/bashref.html#History-Interaction Commented Jul 17, 2013 at 18:31

Not the answer you're looking for? Browse other questions tagged .