Surviving vi

OIBAROS!! GRRR.

Anyone working in a unix environment will encounter vi at least once. Most of the people who do will find it frustrating and counterintuitive, and most will switch to a different editor. However, vi is extremely powerful once you are familiar with it, and after the initial interface shock, it becomes far more intuitive. Knowing vi is exceptionally convenient, because every unix you will likely encounter has a version of it.

To start, vi has 2 operating modes: editing mode and insert mode. This is the main part of the interface shock. vi was created to be used on text only consoles, where selection and more complex operations had to be done from the keyboard; at the time, keyboard layouts varied enough that it had to be used with nothing beyond the main part of the keyboard. Several keys move from editing mode to insert mode: i (insert before cursor), I (insert at beginning of line), a (append after cursor), A (append at end of line), o (insert in new line below cursor), O (insert in new line above cursor). To leave insert mode, press escape.

In insert mode, just type away. If you press cursor keys and the cursor moves, you will not be able to overwrite the text you inserted before that. Some versions of vi, especially on commercial unixes like Solaris and HP-UX, will act very strangely when using cursor keys.

Edit mode is where all of the obscure and powerful commands appear. There are lots of commands, even experienced vi users usually only use a small fraction of them.

Edit mode gives you access to movement commands that are far more powerful than those allowed by most text editors. The arrow keys will move the cursor around, but you can also use h (left), j (down), k (up), and l (right). These keys are also motion commands in the popular console game, nethack. W and w skip a word forward; W will skip by whitespace (ie, to the next space, tab, or newline) and w will skip to the next character that isn't a letter, number, or underscore. B and b act identically to W and w, but they skip back by words. $ moves the cursor to the end of the current line, and ^ moves it to the beginning. { and } will move backwards and forwards by paragraphs, separated by a blank line. % will move the cursor back and forth between any brackets the cursor is currently enclosed in. Finally and most importantly, any movement command that moves in a step (ie, everything but $, % and ^) can be prefixed by a number and the command will be repeated that number of times, for example, typing "15W" would skip 15 whitespaces forward.

Cutting, copying, pasting, and deleting are very flexible in vi. x deletes a character from the cursor position, and X deletes the character before the cursor. s replaces the character after the cursor (this moves you into insert mode). r will replace the character under the cursor with the next character you type. All of these can be prefixed with a number, and they will perform the command on a block that many characters long; for example, 15s will replace the current character and 14 after with whatever you type in, and 15rp would replace the character under the cursor and the 14 after with 'p'. C will replace everything until the end of the line, S will replace the entire current line. "dd" will delete a line, or d followed by a movement command (like 5}, 5 paragraphs down) will delete the text between the current position and the position of the cursor after the movement command. Similarly, "cc" will replace a line, and c followed by a movement command will replace the text spanned by the motion. P and p will paste in the last text that was deleted, P before the cursor, and p right at it.

u is a very useful command: undo. This editor normally has only one level of undo, so be careful. Pressing u again will redo what was just undone. Some clones have added multilevel undo, but it is different in each version. Also, '.' will repeat the last command, be it a large text insertion, or a single character substitution. External commands can be called to operate on blocks of text by using ! followed by some movement command, which will give a "!" prompt at the bottom of the screen, where you type in your command. This command is run in a shell, so if you know how to script in your shell, you could include that in the command. For example, moving the cursor to the top of a paragraph, and typing "!}sed -e s/moo/cow/g" will replace "moo" with "cow" everywhere it appears in that paragraph. Another useful key is ˜ which will toggle the capitalisation of the current character.

Some of the general behaviour of vi can be controlled, as well. From edit mode, pressing : will bring up a kind of command prompt. The most frequent commands are w (write), q (quit), and wq (write and quit, which can also be "ZZ" from edit mode). You can open other files from here with :e filename as well. From this command prompt you can also use the set command, which allows you to activate various features, such as autoindenting and (in some versions) syntax highlighting. For example, ":set autoindent" will turn on automatic indenting. With this, pressing enter will move the cursor one line down at the same level as the beginning of the current line. Lines started by o and O will also indent properly. In some versions of vi, usually the ones in commercial unixes, backspace may not move the cursor back a level; use control-d instead. In some commercial vi's, autoindenting is broken. Sadly, the CEB 531 lab is one of those places. To change the size of tabs, use ":set tabstop=n" to make tabs n spaces long. Syntax highlighting is not a standard vi feature, but many vi clones, such as vim and elvis, have added it; consult their documentation to learn how to activate it. ":set all" will give you a list of options you can set.

This is a small sampling of the commands in vi, there are many more for handling editing several documents simultaneously (an impressive feat for a console editor), limited ability to compile code without quitting, and a significant number of other general commands that I have not mentioned here.

One warning for the use of vi: do it too much, and you will start to like it. If you reach the point where you find yourself downloading versions of it for every operating system you run, you may want to consult a physician (btw, www.vim.org has a win32 command line and gui version of their vi clone. Many linux distributions use Elvis, which also has a win32 version, but most unix hacker types prefer nvi for most closely holding to the feature set of the vi included in 4BSD).


Group Members:

Group Appnotes:

Oibaros frightens your children thanks to the imagination of Cil; check out her site and buy a Flatkitty mug! If you're looking at this with any browser whose version is 4 or lower (except Mozilla) then you probably need help.

Valid CSS! and Valid XHTML 1.0!