Worthwhile vim tips
David Wolever is the man who made me productive in vim. Not from the ground up, mind you. I already knew the basics from my internship like how to swap between modes. I’d internalized that each string of keystrokes forms a command with a verb[0] and, if applicable, movement keys. It was enough to use as a lowest common denominator if I had to edit a configuration file on a server but I was fooling myself by thinking I knew enough to use it as a development environment.
Here he puts into writing the same tips he gave me last year when taking an operating systems course together. (It was the first time I had to use vim on a project of a considerable size.)
It’s come up a few times, so I’ll just go and post it here: the things, in no particular order, I believe to be the bare minimum you should be able to do while you’re editing source code (and how to do them in Vim): [Easily move between files, jump-to-definition, and search for/highlight the current word.]
The first two tips on movement between files, and jumping to function definitions are the most important and have turned vim from small project editor (like, oh, university assignments excepting the operating systems course) to an editor that I can use on moderately sized projects. It doesn’t take that much practice to use the :tag command but I still fumble when using the hjkl movement keys. David was right to encourage me to use them over the arrow keys, though, as it’s the convention used in just about all of the vim commands.
In the same spirit here’s a couple things that I wish someone told me about while I was still an intern, using vim to edit config files because it was the only thing available on those Solaris boxes. They’re basic where David’s tips are intermediate, but they’re also essential:
Know your movement keys
And not just hjkl or the arrow keys. Vim likes to have upper and lowercase characters mean different things but it doesn’t always hold in the case of movement keys. Know that “b” goes to the beginning of a word and “e” goes to the end of the word. Meanwhile, “g” goes to the beginning of the document and “G” goes to the end of the document. These can be combined with commands like delete. The [t]ill movement key is especially helpful there, and I often find myself typing “dt(” which means “delete until the ( character.” Lastly, you can easily jump to a specific line number mentioned in the trace by prefixing it with a colon, so skip to line 13 with :13.
Use set
Even today half the time that I use vim it’s on someone else’s machine. The easiest way to turn on line numbering is to type “:set number” which will toggle it on. To toggle it off you use “:set nonumber”. There’s no meddling with someone’s .vimrc this way and it won’t persist, so no worries about upsetting anyone’s preferences.
Similarly, you can use set to change values of settings, like change the tabstop to five spaces with “:set tabstop=5″. This is especially useful when editing someone else’s python files where they use a different tabstop than you, and you don’t want to permanently change your tabstop but just want to edit their file.
String search and replace
Okay, now you’ve got line numbering on, so you can do a search and replace. It works based on the string search and replace regular expression. You have to specify a range, but there’s a convenient shorthand for a range of the entire file.
For lines 5-15 inclusive, you can replace “fizz” with “buzz” by:
:5,15 s/fizz/buzz/g
And to replace fizz with buzz on the entire file, use:
:% s/fizz/buzz/g
Using the regular expression here can be quite powerful, so if you’re not aware then I suggest you read up on it. If not, know that the s denotes string search and replace, the first thing between the slashes (fizz) is what you’re replacing, the second (buzz) is what you’re replacing it with, and the “g” means that you’re doing a “replace all.” Remove the trailing “g” if you only want to replace the first instance from the cursor.
And it’s not related to vim, but you can do a string search and replace on several files by using a command similar to:
perl -pi -e “s/string_to_replace/string_to_replace_with/g” *.conf
Related articles by Zemanta
- Emacs ‘mode’ and learning `modes` (zzzoot.blogspot.com)
[0] Wincent.com has a writeup that includes similar observations, and contrasts to Emacs.
4 Comments to “Worthwhile vim tips”
Leave a Reply

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_c.png?x-id=1768ccac-669c-4e64-a95e-a5e7b975a90c)






That’s a good set of things every Vimmer ought to know… Although, how often do you use search-and-replace over a range of lines (ie, :5,15s/foo/bar)?
I use it maybe 10% of the time, generally when I’m splitting a method into separate methods while refactoring so I only want to replace some of the method calls.
I also recall a few places where doing piecewise regexps was more convenient than writing one big one.
Ah, ok. Normally I do it by selecting the lines in visual mode or visual-line mode, then `s/` over that.
Just have interest on that, but not so familiar with that, trying now.
Learning, trying, …