Vi(m)

A bit of history

The original vi editor was written as a fullscreen extension of the ex editor on Unix at UC Berkeley in the mid-1970s by Bill Joy (later one of the founders of Sun). Because it's so much lighter-weight than Emacs, it will be found on pretty much any Unix system in existence. Some Unix standards even require it. These days you aren't likely to actually encounter vi, but rather one of the many reimplementations of it. By far the most common is called Vim (vi improved), and this is installed on all of the computers in the HLP Lab. Vim comes with a terminal-based version simply called vim and a version with a GUI called gvim.

Usage

Like Emacs, Vim is a very powerful and sophisticated editor that can be intimidating if you aren't used to it. Most regular users of Vim have their own .vimrc file full of customizations to make it behave as they prefer, much like Emacs users and their .emacs files.

The hardest thing for new users to understand with vi is that it's a moded editor. When you start the program it is in command mode, which doesn't allow you to type; it only accepts commands. If you want to type, you have to hit i in command mode to enter insert mode. The commands are designed to allow you to keep your hands on the keyboard of a terminal that has no function keys, no arrow keys, and no numeric keypad. It was developed on a terminal where Esc was where Tab is on most modern keyboards and Control was where Caps Lock is. A further complication comes from vi being developed as an extension to ex, leading to it accepting vi and ex commands, which can be rather different (e.g. to save and quit: the ex command is :wq whereas the vi command is ZZ).

At first modality seems insane to most people, but the more you get used to it the faster and more efficient you get at editing tasks. Part of this comes from most commands taking an optional numeric prefix of how many times to repeat them. A simple example: Type 200j (in command mode) to move the cursor down 200 lines.

Quiting

A helpful first thing to know with any editor is how to quit. The first thing you need to do in vi to quit is to make sure you're in command mode. If you don't know, hit Esc a couple of times, until the computer beeps. Type :q to quit. Type :wq to save and quit. Type :q! if you want to quit without saving your changes to the file.

Help

Type :help followed by a keyword to get help with any command, or :help alone to get the help index. For a nice tutorial, type vimtutor at the command line instead of vim.

Saving

You've already encountered how to save a file in the section on quiting, although it was embedded in a quit command. Type :w to save a file. If it's the first save for a new file you need to follow the command with a name for the file.

Opening

There are a number of different ways to open a file, particularly in vim. The most basic is :e (short for "edit") followed by a filename (with path if necessary). Use :split (with optional filename) to open multiple files in the same window. You then use ^w w (where ^ means the Control key) to toggle back and forth between the buffers. Vim 7.0 has added the capability to have tabs. Type :tabe followed by a filename to open a file in a tab, :tabnew opens a new file in a tab, :tabn or ^PageDown to go to the next open tab, :tabp or ^PageUp to go to the previous open tab.

Vim isn't perfect at opening files with the right character encoding. (It's a very difficult problem to solve.) To force an encoding, use ++enc= with an open command (e.g. :e ++enc=gb2312 zhongwen.txt to open the file zhongwen.txt with the encoding set to GB2312, which is a common encoding used in China)

When opening a file from the command line you can add an optional parameter of + and a number to open the file with the cursor at that line (e.g. vim +234 foo.txt to open foo.txt with the cursor at the beginning of line 234).

Movement

To move around the screen you can always use the h j k l keys on the keyboard for left, down, up, and right respectively. You shouldn't have to use these if you don't want to though, as most properly configured modern systems also allow you to use the arrow keys as well. But once you understand the vi philosophy, you'll find that using the letters allows you to be more efficient in editing. Two other helpful keys for movement are ^ (beginning of the current line) and $ (end of the current line), which should be familiar to anyone who has ever used regular expressions. If you know what line of a file you need to go to then you can simply type : followed by a number. Programmers find this particularly useful, because compilers generally tell you what line they encountered an error on. Another movement key of use to programmers is %, which jumps the cursor to the matching bracket when it's on a bracket (i.e. something in the set ( ) { } [ ]). Recent versions of Vim also highlight the matching bracket when cursor is over one.

vimrc

As mentioned above, most people customize how they use vim with a file in their home directory called .vimrc (or _vimrc on Windows). In this section are some useful and common lines to have. For more ask your local Vim guru or search on google, as many people put their vimrc online.

Vim (last edited 2012-11-07 16:29:37 by echidna)

MoinMoin Appliance - Powered by TurnKey Linux