header

Torsten Curdt’s weblog

Bang command

I cannot remember where I found this and I always forget to use it. But this so called “bang command” should save a lot of typing. Let’s say you executed…

% which firefox
% make
% ./foo -f foo.conf
% vi foo.c bar.c

Now by using the “!” you can access tokens from the previous command.

Full line:     % !!            becomes:   % vi foo.c bar.c
Last arg :     % svn ci !$     becomes:   % svn ci bar.c
All args :     % svn ci !*     becomes:   % svn ci foo.c bar.c
First arg:     % svn ci !!:1   becomes:   % svn ci foo.c

Or you can even use a search pattern.

Full line:     % !./f          becomes:   % ./foo -f foo.conf
Full line:     % vi `!whi`     becomes:   % vi `which firefox`
Last arg :     % vi !./f:$     becomes:   % vi foo.conf
All args :     % ./bar !./f:*  becomes:   % ./bar -f foo.conf
First arg:     % svn ci !vi:1  becomes:   % svn ci foo.c

Pretty nifty.

  • Jonathan
    FYI, another good link: http://www.washington.edu/comp...
  • Bangs are nice indeed, I keep spreading the word everytime I can. However a little nitpicking doesn't harm: the first arg can be referenced more easily with !^
  • oldtimeunixguru
    I think bang "!" came from tennex shell (tcsh) from University California Berkley Unix. It then was integrated into csh and now bash. The next logical step is to use Ctrl-R for recall command. Just type any string at the command line - including args and then type Ctrl-R and it will recall the first matching command and put it on the command line. You can also scroll through all matching commands - see the bash man pages for more info. Once you start with Ctrl-R you will stop using bang "!" except for those rare occaisions - like !rm returning rm -fr . and you happen to be in the root directory. Doh!
blog comments powered by Disqus