Icon_search
Colin

Useful (though random) vim tips

Posted on 01/04/2009 by Colin
 | 

Following Thoughtbot's excellent example, I thought I'd also put together a few random vim tips for others who've made the scary jump from Textmate.

Searching

Assuming you've got your fingers dirty with vim's great search functionality (faster to use than Textmate's, I might add), you may encounter occasional unpleasantness when you've just searched for a word or pattern that occurs multiple times throughout a file: too much highlighting (wha?!? is such a thing possible?). It can get annoying if you're trying to skim through the text after you're done with the search and have done whatever you needed to do with the results, especially if there are tons of results and you've searched globally, with something like this:

:%s/ */ /g

For those less familiar, here's what's going on:

  • : puts you in ex mode (ex is the line editor that vi and vim are built on top of)
  • % designates all lines of the file (1,$ does the same thing)
  • s/ */ /g searches for all occurences on each of the designated lines (because of the g, and the % above) of the space character, followed by 0 or more of them (/ */), and replaces those occurences with a single space.
  • If you're totally lost, check out a regular expressions primer such as http://www.regular-expressions.info/

At any rate, doing this on most files in vim (assuming your .vimrc file has hlsearch set, as mine does) does the job just fine, but leaves behind a bunch of distracting highlighted space characters. Get rid of them:

:nohlsearch or noh for short.

Your precious search highlighting will be back to normal on the next search. If for some reason you never want search highlighting, you can of course add set nohlsearch in your .vimrc in place of set hlsearch. This highlighting tip is a fairly minor point, but it solved a problem that was really annoying to me for some reason.

Scrolling

It's only been relatively recently that I learned to scroll in vim without moving the cursor. This action is more like the ol' mouse wheel that Textmate users like me knew and loved for "quick" file navigation, only (as with everything else in vim) you don't have to waste time going to the mouse. My favorite version is z., which takes whatever line you're on right to the middle of the screen, where you can view its surrounding context.

Here are some other scrolling actions:

  • z- takes the line you're on to the bottom of the screen
  • z+ takes the line you're on up to the top of the screen
  • Ctrl-y takes the line you're on down one line (so it scrolls the page UP one line)
  • Ctrl-e takes the line you're on up one line (so it scrolls the page DOWN one line)

I'm sure my wording is a little weird with these, but it's best if you just practice doing them and see if they are useful. Note that with the latter two commands (Ctrl-y and Ctrl-e), if you try to scroll the page down when your current line is the top line on the page, your current line will move down to become the new top line on the page. This really makes sense; you rarely will want to scroll a line offscreen that you need anyway. If you do, on the other hand, you're probably better off using a split screen (:sp), which is a feature of vim that Textmate definitely lacks, though it's not one I've personally had much use for.

Hopefully some of this will help someone who's looking to get better at vim --- if you're looking for more, I highly suggest the book Learning the vi Editor by Linda Lamb and Arnold Robbins (O'Reilly). Among other things, their tips and examples for searching are outstanding. I assume that the newest edition (now titled Learning the vi and Vim Editors) is even better, though I haven't had a chance to read it yet

Tagged:  vi, vim, textmate, regex, regular expressions, searching, text editor, ex