Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by jasonwryan, slm, Evan Teitelman, Anthon, rahmu Aug 9 '13 at 8:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

See… –  spuder Aug 8 '13 at 22:49
Looks like the answer is !$ but I usually hit up-arrow,home and delete the previous command and type the new one. –  TecBrat Aug 9 '13 at 4:19

5 Answers 5

up vote 16 down vote accepted

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).

share|improve this answer
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 8 '13 at 22:53
@Drav Sloan thanks for the comment, I have edited the answer –  Miguel de Val-Borro Aug 9 '13 at 0:22

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 !*.



$ echo 1 2 3 4
1 2 3 4

run #1 then this:

$ echo !$
echo 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
$ echo !1059:3
echo 7
share|improve this answer
You can also get the nth argument using !:n, so with your example echo !:3 will echo 3. –  Drav Sloan Aug 8 '13 at 22:54
@DravSloan - yes check out the 2 links I posted. Those Q&A's cover it ALL! –  slm Aug 8 '13 at 22:57

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.
share|improve this answer
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 9 '13 at 0:08

Use ESC + .

It will grab the last argument from the previous command

You can also do a search and replace

less /foo/bar.txt
share|improve this answer
Do you mean Esc+. ? –  Miguel de Val-Borro Aug 9 '13 at 0:27
@MigueldeVal-Borro Whoops, yes. Dyslexified that. –  spuder Aug 9 '13 at 0:40

Another possibility:

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.

share|improve this answer

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