Bang Bang

BangBang is one of the OldTimer UnixHack?'s names for a character sequence typed as !!

To many UnixShells, !! means 'do the last thing/line again'. Shells supporting !! include bash, csh, tcsh .


As a method of UnitTesting, it can be used like this: Let # denote comments for the wiki and $ denote a shell prompt

$ flub this | gnot that
# hmmm, gnot didn't do what I wanted here, I need an option
$ flub this | gnot -t that 
# ok, but it said the option was unrecognized.
$ co -l gnot;vi gnot;!! 
# lock the file, edit the file, and run the previous test again. # but the above command breaks !! and if your shell doesn't support command line editing, that's a bad thing # a better way to have said the above, would have been the two lines:
$ co -l $HOME/bin/gnot
$ vi !$;!-2
# the UnixHack?s know that !$ is the last word on the last line and that !-2 is the line I typed two commands ago. # now you can keep doing !! to do both the FixIt and the UnitTest until you get the desired result
$ !co  
# asks if you're sure, ^C saying No. We only did this to get !$ back
$ ci -u !$
# When the code does what you want, checkin the final product, and get on with it: use the result your original query tried to obtain. This iterator often takes less than ten minutes, and will save you that amount of time on the third or fifth time you use the new tool or feature.


As a tool for refactoring (see WhatIsRefactoring) BangBang can be used as below:

$ grep SomeClass *
Foo.h: class SomeClass {
Bar.cc: SomeClass sc;
...
$ vim +"!!"

...to decode:

  1. vim takes an ex command on the command line after a + (so vim +q would start the editor then run :q, which would quit.)
  2. It happens that vim has an extended ex command :grep, which parses the output of the grep and edits each of the files that matched, on each line that matched.
  3. The above line expands to vim +"grep SomeClass *"

This is one of many tricks that helps you to kind of surf between the various tools. Long Live Unix, eh?

Of course, there's no need to use special capabilities of vim to do this. In the traditional unix manner, you can use more general facilities of /bin/sh to make this work with any program:

nvi `grep -l FooBar *.java`

or, to get all of the files in a tree:

nvi `find . -name \*.java | xargs grep -l FooBar`

Vim actually jumps to each matching line of each matching file (you jump through the matches with ":n" and friends (see ":help :n")). The above nvi version isn't entirely terrible, it's just not entirely fast. Another problem is that you don't get a preview of the text. Furthermore your above version (without the use of BangBangs) doesn't give you a preview of even the files themselves (though that's easily correctable).


EditText of this page (last edited November 15, 2014) or FindPage with title or text search