From LQWiki
Jump to: navigation, search

vi is a text editor originally written by Bill Joy for Bell Labs, subsequently sold as for use with AT&T Unix. It is unique in that it has two primary modes, a command mode and an edit mode. A version of vi can be found on all Unix and Unix-like systems including Linux, and its existence is assured in the POSIX standard. It is therefore useful to know some basic commands.

Emacs vs. Vi or Vim

Vi vs. Emacs is the subject of a holy war, but this has largely been settled by the rise of WYSIWYG editors, which have tended to displace both options, and faster hardware, which renders speed differences irrelevant. Moreover, there may originally have been much more to fight about, but with the addition to Vim of many features similar to those of Emacs, the differences for many people are a matter of taste: an editor with modes to remain aware of (Vi) or one with arcane key combinations to learn (Emacs).


Since vi is a modal editor, one has to switch modes between entering commands and entering text. While this may sound tedious at first, as one's experience grows with the editor, this becomes quite natural. Operation usually begins in command mode and (plain) text entry commences with a suitable command (such as a, i, o or their upper-case variants.) Pressing the escape key ends the entry of text, and puts the program back in command mode. Commands can often be prefixed by a number to represent repetition of that command by that number (for example, dd deletes one line, 3dd deletes three). Marks and movement commands can be used to operate on larger chunks of text.

There are a few other modes, less often used but still important: visual mode, and ex mode for example, each with its own set of capabilities.

Basic commands

Some of the more common commands and their meanings:

i - insert (before the current character)
a - append (after the current character)
o - open a new line (below the current one) and insert text there
x - delete one character
^ - position the cursor on the first printable character of the current line
$ - position the cursor at the end of the line
0 - position the cursor in the leftmost column
:w - save (write)
:wq - save (write) and quit

Other useful commands

ZZ - another way to save and quit
:q! - quits without saving the file
:w - saves the file (note that appending q will save and then quit)
:w path/filename - save the file you are working on in the directory path called filename
/some text - searches from your current position forward for "some text"
             pressing / again will search for the last thing you searched for.
             To search backwards, use ? instead of /


Vi also has some handy ways of moving around in a document. In command mode, the letters h,j,k,l will move the cursor left, down, up, right respectively. 0 (zero) will move you to the beginning of the line and $ will move you to the end of the line.

If you have a more modern keyboard with arrow keys, the bindings are set up so you can use these as well. However, many experienced vi users do not use the arrow keys - navigating can be done without having to move one's hands a great distance.


d^ - Deletes from the current position to the beginning of the line
d$ - Deletes from the current position to the end of the line
dw - Deletes from the current position to the end of the word
3dd - Deletes 3 lines from the current position downwards
d/x - Delete up to (but not including) the first occurrence of x after the cursor position


u - Undo the last change

In the original vi only the last change can be undone.


y^ - Copy from the current position to the start of line
y$ - Copy from the current position to the end of the line
yw - Copy from the current position to the end of the word
3yy - Copy 3 lines in downward direction from the current position.
y/x - Copy up to (but not including) the first occurrence of x after the cursor position


p - Pastes after the cursor
P - Pastes before the cursor
np - Pastes text n times

Clone versions

The original vi did not have sources available and was not free, but now, has been relicensed and is available for download. Because the original vi was not free, clones were created.


vim is probably the most popular vi clone and most Linux distributions come with vim. Vim is an improved version of the vi editor distributed with most UNIX systems. Unlike the original vi, it also has a graphical version called gvim. Vim comes with a tutorial on the basics, which can be started by running the command 'vimtutor' in the shell.

Vim isn't a word processor. Although it can display text with various forms of highlighting and formatting, the formatting is just on screen and it still saves the file as plain text. If you wish to use it as a word processor, you will need to use it in combination with a typsetting language such as TeX or LaTeX.


Short for new vi, it was developed by Keith Bostic at University of California at Berkley. In the early 1990s, BSD developers wanted a vi whose source code could be freely distributed. They started with elvis 1.8 though little elvis code still exists. nvi is the de facto vi clone under BSD systems.

nvi is very close to the original vi but with the original limits removed and some features added: split screen editing; file completion; infinate undo/redo and overstrike mode. Nvi does not include many of the little used features that exist in VIM. It is lighter than VIM.


elvis was written and is currently maintained by Steve Kirkendall. An early version (1.8) was the basis for nvi. It is available on many operating systems.


vile was written by Paul Fox, Kevin Buettner and Thomas Dickey (who is the maintainer). It is available for many operating systems. Unlike the other vi clones it does not try to be precisely vi, but has an Emacs flavor.

External links

See Also