h1

history substitution vim trick

April 15, 2007

One of the tricks I’ve been using recently is:


vim `!!`
  1. ` (backtick) which is used on the command-line to substitute, in-place, the result of a command
  2. !! (double-exclamation) which is replaced by the history mechanism by “previous command”

In short: open in vim the result of the previous command, which would be a list of files.

When I’m programming I always have a shell open. I use that shell to find (pun intended) interesting files.

Here are some commands I could run iteratively until I find the proper set of interesting files. Note that I would usually only include the -l flag to grep after I have confirmed the content.


# all files, recursively, containing 'each'
grep -Ril 'each' *

# all rhtml files, recursively
find . -name '*.rhtml'

# all ruby filers, recursively, containing 'each'
find . -name '*.rb' -print0 | xargs -0 grep -Ril 'each'
# -print0/xargs -0 credit goes to dmiessler's find page

# all subversion files containing conflicts
svn status | awk ‘/^C/ {print $2}’

On the command-line, you are only limited by your knowledge and your imagination.

If you are unsure what good these commands are to you, because you have “better tools”, I invite you to read Oliver Steele’s The IDE Divide.

Leave a Comment