Friday, July 18, 2008

Vim Tips, part 1

Because I'm such a fan of Vim, and because I have people asking me about it quite often (for tips, general questions, etc.), I figured I'd write a two-part article on the greatest text editor in the world.

There are innumerable key combinations in Vim (well, they are in fact finite, but numerous), so I thought I'd describe the ones that I use most often and find the most useful:


  • h, j, k and l - Move left, down, up, or right.

  • gg and G - Move to the top or the bottom of the file.

  • ctrl-u and ctrl-d - Move up or down one half-screen.

  • w and b - Move forward or backward, one word at a time.

  • fX, FX - Move the cursor over the next or previous occurrence of X in the current line, where X is any character.

  • tX, TX - Move the cursor to (not over!) the next or previous occurrence of X in the current line.

  • 0 and $ - Move the cursor to the beginning or end of the line.

  • ^ - Move the cursor to the first non-whitespace character of the line.

  • /[string] and ?[string] - Searches forward or backward for [string].


I love tabs. They're a new feature in Vim 7, and I instantly became dependent on them. Here are the commands for using them:

  • :tabe [file] - Open a new tab editing [file]. Don't specify [file] to open a blank tab.

  • gt and gT - Move to the next or the previous tab. If already at the last or first tab, wraps
  • around.
  • Ngt - Move to the Nth tab.

  • :q - Closes current tab.

  • :qa - Closes all tabs and exits Vim.


  • d - Deletes in the direction of your movement. E.g.: d$ deletes to the end of the line, dw deletes to the end of the current word, dF[ deletes backward to and including the next '[' character.

  • dd - Deletes current line.

  • rX - Replaces current character with character X.

  • ~ - Toggles capitalization of current character. If in visual mode, toggles capitalization of highlighted block.

  • s - Deletes current character and enters insert mode.

  • i - Enters insert mode.

  • a - Moves one character forward and enters insert mode.


Folding is incredibly useful. It's great for getting large blocks of code out of your way in annoyingly long class files.

  • zf[movement] - Folds lines in direction of [movement]. E.g.: zfG folds all lines from the current line to the end of the file. For [movement], you can also use text objects.

  • zf - In Visual mode, will fold the currently selected lines.

  • zo - Open the fold under the cursor.

  • zc - Close the fold the cursor is in.

Other Tips

  • :% - Replaced by current file. You'll see how this is useful below.

  • :![command] - Execute [command] in the shell. E.g.: !php % - runs current PHP script, !ls - lists files in current directory, !php -l % - checks syntax of current PHP script, !cp % %.bak - creates a backup of current file.

  • :nohl - Turns off highlighting, if it's on. Depending on your Vim's configuration, doing searches (with / and ?) highlight all matches. This will un-highlight all search matches.

This may seem overwhelming if you're new to Vim, but it becomes second-nature as you get used to it. I did not have to check the Vim documentation at all to write this blog post. It is in fact possible to become accustomed to using so many (useful!) Vim commands. Of course, these aren't all I use, but just the most common.

Next post, I'll discuss my .vimrc file, which contains tons of configuration that makes using Vim a lot easier for me. Perhaps you'll be able to use some of my settings to help you, too.


marilyn c. cole said...

Lucas, it's great to see a friend from back before I was a computer person has also evolved to be a vi-lover and proper geek! Haha. I love this post because it's cool to see some tricks that I don't use very often and should (folds, which I only ever use with vimdiff, and fX/FX/tX/TX). The only thing that I use a lot and you don't mention are the various blocking tools -- shift + v to highlight lines, ctrl + v to highlight vertical blocks, and v to highlight sequential chunks. AND you can change the blocking style in the midst of your moving the cursor, which I only recently realized.

You may have just left that stuff out because you already had too many "basics," though, hm?

Anyway, like the blog! Post more and maybe I'll try to post more geeky stuff on mine which is just full of vaguely literary ponderings.

Anonymous said...

Thanks for your great tips ... I didn't know about the tabs, they're really useful!