30

Here's something I find myself doing often:

less super/long/file/name

Followed by:

vim super/long/file/name

Is there an easy way to pass the args of the previous command over to the next? SO I'd like to do something like

vim !!!

And have it automatically open super/long/file/name in vim.

  • See http://superuser.com/questions/621012/pass-previous-commands-arguments-to-current-command – spuder Aug 08 '13 at 22:49
  • 1
    Looks like the answer is !$ but I usually hit up-arrow,home and delete the previous command and type the new one. – TecBrat Aug 09 '13 at 04:19

5 Answers5

35

If you just want the last argument from the previous command then use !$. If you want all the arguments from the last command then use !*.

Example

COMMAND #1:

$ echo 1 2 3 4
1 2 3 4

run #1 then this:

$ echo !$
echo 4
4

run #1 then this:

$ echo !*
echo 1 2 3 4
1 2 3 4

Also I highly recommend that you check out this Unix & Linux Q&A & @Gilles' SuperUser Q&A:

Colon syntax

You can think of the history output as a grid. Each command is a row, each argument of each command is a column. Like this: (!line:column).

So you can reference previous pieces of commands like this:

$ history | grep "105[8-9]"
 1058  echo 1 2 3 4
 1059  echo 5 6 7 8

$ echo !1058:2
echo 2
2
$ echo !1059:3
echo 7
7
slm
  • 369,824
30

Using !$ should work to access the last argument of the previous command in the bash shell:

less super/long/file/name
vim !$

Also Meta + . or Esc + . can be used to paste the last argument if the readline library is enabled in emacs mode (default option).

Braiam
  • 35,991
gypaetus
  • 921
  • Bash defaults to readline in emacs mode which is why the Meta-. trick works. This will not work if you are another mode (such as vi ... set -o vi), you can get readline into emacs mode with set -o emacs. – Drav Sloan Aug 08 '13 at 22:53
  • @Drav Sloan thanks for the comment, I have edited the answer – gypaetus Aug 09 '13 at 00:22
9

Miguel de Val-Borro gave the correct, general answer for your question, but since you said you often do this particular sequence of less and vim, it's worth mentioning that you can press v inside less to open your configured editor. From the less manual, under v:

Invokes an editor to edit the current file being viewed. The
editor is taken from the environment variable VISUAL if defined,
or EDITOR if VISUAL is not defined, or defaults to "vi" if  nei‐
ther VISUAL nor EDITOR is defined.
Braiam
  • 35,991
Paulo Almeida
  • 746
  • 3
  • 4
  • 1
    Nice! Note that exiting vim in this situation returns you to running less on the file (rather than returning to the command line). – Greg Marks Aug 09 '13 at 00:08
5

Use ESC + .

It will grab the last argument from the previous command


You can also do a search and replace

less /foo/bar.txt
^less^vim
spuder
  • 18,053
0

Another possibility:

r=super/long/file/name
less $r
vi $r
ls -l $r
chmod 600 $r
cat $r >> some/other/even/longer/file/name
...

To save typing, you should be able to enter super/long/file/name, etc., using tab completion.  There is, of course, nothing special about the variable r; I chose it simply for proximity to the dollar sign key.

Greg Marks
  • 1,811