Log in / create account | Login with OpenID
DocForge
Programmer's Wiki

Vim

From DocForge

Vim, or Vi IMproved, is the enhanced GNU clone of the classic Unix text editor vi.

Bram Moolenaar actively maintains Vim. The current version is 7.1.

Contents

[edit] Charity

Vim is charityware: While Vim itself is GPL'd and freely available, there's also a book, and proceeds from book sales go to help orphans in Uganda via ICCF Holland. Vim users are also encouraged to donate to ICCF Holland if they find Vim useful.

[edit] What's IMproved?

Many things are improved, though Vim can also be set to maintain compatibility with the original vi's behavior. To break this compatibility, type :set nocompatible in vim or add that line to your .vimrc file.

  • Syntax highlighting & formatting: Everyone knows a text editor is worthless without syntax highlighting. Having it do automatic indentation appropriate to the programming language you're using is a big win, too. Vim does these.
  • Multi-level undo: vi only allows single-level undo; Vim tracks each change during your session, so u in Normal mode always goes back one change in the history, while Ctrl-r redos each undone change in the same order. Also, Vim 7.0 implements time travel: :earlier travels back through the change history by a specified amount of time, while :later goes forward in time (within reason, of course -- :later 3 months on a blank screen will not magically complete your project for you).

[edit] Customization

You can save personalized Vim settings in your .vimrc file. That's simply a text file containing a series of Vim commands, saved as .vimrc in your home directory on Linux or Unix systems, and _vimrc in %ProgramFiles%/Vim/vimfiles/ on Windows systems.

All of the statements in a .vimrc can be executed from Vim's command line as well, and vice versa, so it can be helpful to study an example .vimrc to learn exotic commands for customizing Vim's behavior. Note that vi is a standard editor on Unix systems, so it's worthwhile to get used to the default behavior before you change the keybindings around in case you find yourself needing to edit text files on a system you don't own.

[edit] Tips

These are helpful suggestions -- they're not rules, and they may not even be useful to you. But that's what a wiki's for, isn't it?

[edit] Ergonomics

  • If you're on a standard keyboard, don't use Escape to get back to Normal mode. Use Ctrl-[ or Ctrl-c. The keyboards vi and ex were designed for had the Esc key where your Tab key probably is, reasonable close to the home row -- unless you've remapped your keys, Esc is now annoyingly far away, so it's faster to use the alternate bindings.
  • Swap the left Ctrl key with the Caps Lock key in your operating system's keyboard settings. This is straightforward in Gnome and KDE, and a more common operation than you might think. (Reason: You'll use Ctrl a whole lot more than Caps Lock in Vim, and probably in general. Plus, in Vim, accidentally having Caps Lock on can be devastating in Normal mode, and there's another way to capitalize a region -- gU). You might consider moving Escape somewhere closer, too (e.g. where Tab is), but I recommend just using Ctrl-[ instead.


[edit] Learning Commands & Customizations

  • Use the help system. F1 brings up the main help text, or use :help [topic] to look up the topic [topic] (fill in your own). The documentation for Vim is actually unusually good, and gets to the point quickly.
  • Poke around online for example .vimrc files. Be a bit conservative in incorporating script segments into you own .vimrc until you've figured out how they work, otherwise you'll just have an oddly behaving editor and no additional knowledge. Many of the commands have subtle effects that may be difficult to pinpoint after the fact -- again, look up what a command does, then decide how you want to use it.
  • Look on Vim.org for scripts and more tips.

[edit] Managing Multiple Files

  • Use :split, Ctrl-w s (horizontal), :vsplit or Ctrl-w v (vertical) to split the editor window into two frames. Ctrl-w = makes the frames the same size, Ctrl-w Ctrl-w jumps to the next frame (clockwise if there are more than two frames) and Ctrl-w o closes all the frames except the current one.
  • Use :buffers to list all the files currently open, and :b [#] ([#] = buffer number or filename) to jump to that buffer. Note that this is even better than using an IDE's tabs: Tabs get cluttered after a dozen or so files and require using the mouse, while a list of dozens files is readable at once and still manageable from the keyboard.
  • Use ctags to deal with C code logically.