An Introduction to Vi

A long time ago, in a college far far away, some nerds were playing with UNIX. At that time, UNIX shipped with ed. Some rather clever programmers made a replacement for ed called em. Em became en. En became ex. Ex is Vi. Why is this important? Understanding where Vi comes from, helps you to understand its rationale. Why does Vi matter? It's an editor that has become part of the UNIX specification. Knowing Vi means that you know at least one text editor that is present on nearly all UNIX-like operating systems (ones like: most Linux distributions, *BSD, AIX, HPUX, Solaris, OSX, etc...). This makes Vi the defacto UNIX editor. Also, while it may take time to familiarize yourself with Vi, once you know it you can be insanely efficient.

All of the commands I will be going over are from strict Vi implementations, and ought to work in any Vi clone you wish to use (traditional, vim, elvis, nvi, busybox vi, whatever). Some commands will not be mentioned due to their lack of universality. Only those ex commands that are absolutely necessary will be mentioned in this introduction as well.

Vi is rather spartan in comparison to something like Emacs. There are no menus or toolbars. There are absolutely zero in-application help functions. Vi does not use ctl, alt, meta, home, end, or any other such keys. So how is Vi powerful if lacks so much? Vi is modal. Being the visual interface for a line editor, having modes is both rather handy and somewhat necessary. Like ed and ex, the editor does not start in insert mode. It starts in command mode. Command mode is useful for anything that does not involve typing text into a file. Line command mode is entered by pressing ":" and exited by pressing "esc". Insert mode is most easily entered by pressing "i" and exited by pressing "esc". Why esc? The computer on which Vi was created had the escape key where a modern keyboard's tab key is. Why two different command modes? Line command mode is used for ex commands, command mode is used for navigation and a few other functions (cursor positioning/find/replace/copy/insanely precise delete/cut/etc...). And, of course, the insert mode is used for inserting text. Exiting Vi, one has a few different options. Enter the line command mode, and type "q!" to exit without saving ("q" if no changes were made to the file). Typing "wq" (w == write, q == quit) or "x" in line command mode will exit and save.

A word of warning before we get into various commands: in Vi a command will execute as soon as you type it. Only line commands for ex require a press of the enter/return key. So what are some of the more basic commands for Vi?

Let's start with basic document navigation:

h == move left
j == move down
k == move up
l == move down
( == move back a sentence
) == move forward a sentence
{ == move back a paragraph
} == move forward a paragraph
0 == beginning of line
$ == end of line
1G == first line of file
G == last line of file
H == top of screen
M == middle of screen
L == bottom of screen
w == next word
b == beginning of word
e == end of word

None of those commands will alter text. They will merely move the cursor/user through the document. Note that those are just the basic navigation commands, and you can already see how useful their abilities can be. Rather than using arrow keys to go one character space at a time, you can jump to various spots within a document immediately.

Inserting text is equally as quick. I also find Vi especially wonderful to work with because of its insert features. I will open up Vi, and press "i" and begin typing. After a while, I may realize that I want to type a new paragraph between the one I am writing and the one before it. So, I press "{" and then "o".

i == insert before cursor
I == insert before line
a == append after cursor
A == append after line
o == open a new line after current line
O == open a new line before current line
r == replace one character
R == replace many characters

Text deletion is an easy enough affair.

x == delete to the right of the cursor
X == delete to the left of the cursor
dd == delete a line

Deletion can also be accomplished through motions:

xw == delete word
x0 == delete to beginning of line
x$ == delete to end of line

Copy and paste in Vi is referred to as yank and put, and like deletion these can be used in combination with motions.

yy == yank the current line
yw == yank word
"0" followed by "y$" == yy
p == put after current position
P == put before current position
/foo == search forward for 'foo'
?bar == search backward for 'bar'
n == next match to most recent search
N == previous match to most recent search

There are many more commands for Vi. These should be enough to replicate the function of most editors... which is funny considering that doesn't even scratch the surface of Vi. Vi can handle regular expressions, file joining operations, ex commands, etc... If you are using Vim, Vi also becomes scriptable itself, and can handle things like multiple document viewing/editing. Have fun exploring.


© MMIX - MMXVII absurd.wtf
Licentiam Absurdum