Saturday, May 2, 2009

A surefire shortcut to Insert the Last Argument of the Last Command

Sometimes, in the Linux command-line world, a seemingly trivial technique can turn out to be tremendously useful. Before I discover the Alt-dot(.) shortcut, I type !$ to insert the last argument of the previous command.

peter@tiger:~$ ls -l Windows_20081110102654.log
-rw-r--r-- 1 peter peter 808 2008-11-10 10:26 Windows_20081110102654.log
peter@tiger:~$ cat !$
cat Windows_20081110102654.log
.....
.....
peter@tiger:~$

Then, I discovered that typing Alt-dot achieves the same result. That is, press (and hold) the Alt key, then the dot key.

There are some advantages of using Alt-dot over !$. First, you can actually see the argument immediately and interactively. You can verify that is indeed what you want, edit it if necessary, before you continue the command-line input, and eventually hit Enter to execute the command. With !$, you better have a pretty good memory.

Another advantage is that you can repeatedly type Alt-dot. The net effect is that you scroll back in command history, and display the last argument of each successive command.

After assimilating the shortcut into my command-line work habit, I found that I have been using it a lot. Give it a try!

PS Related entries from this blog:

7 comments:

rrp said...

Thanks for the tip - very useful

Paul Mohr said...

That is truly awesome. Thanks for the tip.

Anonymous said...

What they said :)

Miloss said...

Sounds nice.. but won't work in my uxterm.

It works in Konsole, but I'm trying to avoid local access to that machine (using remote instead - ssh)

Anonymous said...

Damn, so useful, but I wasn't aware of it, I didn't wonder if such a functionality exists!

AprilCoolsDay said...

It seems you don't know this:

Add the following line to ~/.bashrc,

bind Space:magic-space

Now, whenever you press space after !$, it reveals its content. it also works for other expansions like !*, !!, !cat.

Anybody who's gonna introduce !$ or !! to his/her coworker should also mention magic-space.

Curtis said...

Great tip, thank you.

I'm not sure about other terminals, but you can achieve the same by pressing [ESC] and then [.] in PuTTy.