You and Your Editor: The Bare Minimum You Should Be Doing (1 of N)

October 15, 2009 at 12:26 AM | Vim | View Comments

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

All but the simplest scripts require more than one file, so you'll often need to switch between them.

Eclipse has two very useful features here: Quick Access (command+3) and Open Resource (command+shift+r):

quick accessopen resource

And in Vim, I make heavy use of multiple windows (:new and :vnew, using custom shortcuts, c-h, c-j, c-k and c-l to move between them) and multiple buffers, using :b. For example, below, I have typed :b url<tab>, then I use c-p and c-n to cycle through the list of matching buffers (I also have <right> and <left> bound to :bn and :bp, making it easy to flip through buffers if I'm not sure what I want).

windows and buffers in Vim


When you're working with a large project, especially one for which you aren't the only author, it can become nontrivial to figure out where things (classes, functions, etc) are defined. I've found that it's very hard to read source code without a jump-to-definition function because it forces a mental context switch from "figuring out what this code does" to "figuring out where this method is defined", then once I've found where it's defined, "ok, now what was I looking for again?", and once I've finished reading the definition "why am I looking here again?" and finally to "what was the point of this sentence?"

With Vim, I use exuberant ctags to do this.

First, I run ctags -R . to build a tags file, then I access it using :tag, c-] and c-w ] from within Vim. Also useful is c-t: after c-] is used to examine a definition, c-t will take you back to the place you had originally found that definition. Very, very useful.

tags in Vim

(note, for this to work properly, make sure :set tags=tags;/ is set in Vim)

Search for/highlight the current word

Often, when I'm reading code, I'll want to quickly look through all the references to some word in the current file (for example, to see where a variable is referenced or how a function is called). This sounds trivial, but I find myself doing it often. Very often. So give it a try before immediately disregarding it.

I do this in Vim using * and # and :set hlsearch (as a side note, I've also got \\ bound to :set nohlsearch).

For example, if my cursor is over load_BufferedImage, then I hit *, I can quickly jump through all the tests which call it.

Using '*' to search

That's all… For now

That's all for now, but stay tuned – I do love editing text, so I'll probably come back and write some more on this later.

PS: Just to make very sure it's clear, the only editors I mean to bash here are the ones which make it very hard to do the things I've talked about (nano, notepad, etc)… Every decent editor can do these things, you've just got to learn how.

PPS: If you don't already use it, Vim has a great built-in help system. Access it with :help {subject}. For example, :help vnew or :help <c-w>.

PPPS: Todd has posted a followup, Worthwhile vim tips – they are a bit more basic, but even more important than the things I've talked about here.