agaskar.com

May 03 2008

Handy vim tip: ‘pipe’ files into vim tabs.

Every once in awhile I’ll use absolute paths in files on the test server in order to remove all doubt that the path is correct. Of course, when it comes time to deploy, all of these need to be fixed. Now, you can go the long way around, which is to do a grep, then open vim, save/exit (or shell), grep again, until you’ve fixed all the instances. Naturally, there’s a MUCH quicker way that fits into my workflow: from the root directory of your application/web page

vim -p `grep -rl 'subject' *` 
Read a full breakdown after the jump. Let’s start with grep -rl ‘subject’ * to see what this command does. The r flag tells grep to search recursively through directories, and the l flag tells grep to return just a list of files (normal grep output returns an excerpt with the matching lines). ‘subject’ is the search text we want to find — I’ve put it in single quotes so spaces can be used if necessary. Keep in mind you’ll have to escape any quotes that occur in the search text (i usually just avoid them entirely). The * tells grep to search ALL files. We put this entire command in backticks (`) so it’s executed and *then* evaluated by our vim -p command. The -p flag tells vim to open each of these files in a tab. Once vim opens, use ‘gt’ in command mode to move through the tabs from right to left, ‘gT’ to move left to right. Ok, a couple mea culpas:
  • I’d highly suggest doing a grep -rl ‘subject’ * before trying to start vim — you probably don’t want to open 300 tabs in vim if your subject is too broad.
  • Yeah, in this particular case it’d probably be faster to grep/sed this, or even add in a s/original/replacement statement to our command line.
  • So we’re not really using a pipe here — we’re using backticks to evaluate a shell command. You actually DON’T want to try and pipe this — grep -rl ‘subject’ * | vim -p will complain about “input is not from terminal” and then either crash on you or (hopefully) exit. I use ‘pipe’ here only because that’s what I first tried searching for when I was putting together this statement

Comments (View)
Page 1 of 1
blog comments powered by Disqus