The Problem
Having to re-type the right filename with every shell command can get annoying. The longer a filename is, the more upper/lowercase chars it has, the slower it is to do things with the file - grepping thru it, moving it, renaming it, and so forth.
You probably use file-completion (ESC or or TAB key in most shells) to avoid typing the whole filename, but that only works once you've typed enough of the filename to make it unique. Some directories have sets of files that have a large common portion on the front of the filenames, causing you to type a lot of it before hitting ESC or TAB; some help that is.
For example, a C project directory often has ".c" and ".o" versions of every file in the directory. To edit the source file, hitting ESC or TAB really doesn't help, it gets stuck on the .c/.o part, which you end up typing by hand.
Wouldn't it be nice if there was a way to only have to type the filename once, and then specify it on future commands by typing only two characters?
Most UNIX shells today support an ancient "history expansion" syntax that uses the "!" (bang, or exclamation mark) character. This syntax originated in the CSH, and was included in NewCSH, and eventually TCSH.
What's The Secret?
The secret two characters are:
!$
The "!" character is the history-operation character, meaning that you're going to refer to some part of a previous command.
The "$" character means "use the last field of the previous command I just typed." Here we're talking about space-delimited fields; you know, command line args.
Example
Suppose you want to see if the misspelled string "Smiht" exists in your file with "grep". You discover it's there, so you want to edit it to read "Smith":
grep Smiht my_weird_long_filename.c vi !$
See how easy that is?
You think you fixed 'em all when you were in vi, but let's make sure:
grep Smiht !$
Did you miss one? That's OK, edit it again:
vi !$
Another Secret History Code
Another secret history code is:
!:1
This one means "the first argument on the previous line." In other words, if the previous command was "vi a b c", then !:1 would be filename "a". You can use this in combination with !$ to do some awesome things.
Example
Suppose you decided to rename a bunch of web pages to help improve their SEO rank, based on a tip you heard somewhere. Files are named with index numbers currently, you want to give them meaningful filenames, but you don't want to break people's bookmarks to the old filenames, so you want to symlink the old filenames to the new ones.
(maybe you can figure out how I thought up this example? :)
mv 0001.php running_dump_from_cron_silently_fails.php ln -s !$ !:1 mv 0002.php passwordless_ssh_logins.php ln -s !$ !:1
There's no reason to type a long filename like that more than once. And no matter what the filename is, I can refer to it over and over with the same 2-character keystroke. I've typed these history commands so many times over the last 15 years, it just rolls off my fingertips.
Of course, the first time you do anything related to moving/deleting files, you should practice on a test file you don't care about, to make sure it's not going to harm anything!
Other History Command Tricks
You can refer to any argument on the previous line like !:2, !:3, etc.
You can even refer to older commands, like the last arg of the comand you typed 3 places back is !-3:$
Type "history" to see a numbered list of commands. Then you can re-run command 23's first arg like this: !23:1
Refer to all the args of the previous line by !*
Or all the args of command you did 3 commands ago !-3:*
Supported Shells
It looks like the BASH shell also supports this CSH-like syntax. Since most people use either BASH or TCSH these days, these tricks will probably work for you just fine.
If you're still using an older shell like Bourne shell (/bin/sh) or CSH (/bin/csh), I highly recommend moving to the newer ones (BASH and TCSH respectively). All your old familiar commands will still work, but you get a lot of cool new ones. If you use the UNIX command line interface a lot, these commands will improve your productivity, and make your life easier.
More History Commands
In Unix, type "man tcsh" for more info, then look for the section labeled "History Substitution". Or, on the "man bash" page, look for "Event Designator". There are some web pages on the subject, also.
Summary
Once you get the hang of using history commands like !$ and !:1, you'll use them all the time without thinking. In my mind I think "that file" when I type "!$". I don't even remember what file it is, sometimes, it's just "that file I'm working on right now."
In fact, I've been using it this whole time while writing this Trick page. :)
Don't miss the latest unix tips and tricks!
Subscribe to our low-volume mailing list:
Privacy Policy
| Copyright © 2006 Fastech Learning LLC, all rights reserved. |
| Phone toll free 1-866-464-6688, Phoenix Metro area 480-895-6688 |
| Problem with this web site? please let us know |