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
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.
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
!!:$
!$
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)"
?
!*
-- gnu.org/software/bash/manual/bashref.html#History-Interaction
Commented
Jul 17, 2013 at 18:31
~/foo.txt
is printed bylocate
.