Wednesday, April 20, 2011

Vi: An Introduction

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.

9 comments:

Andrew said...

Great intro to vi.

One thing though, on all vi*s I've used (vim, nvi, vile), the delete commands should use d not x. x cuts one character, while d is used for other delete commands:
dw == delete word
d0 == delete to beginning of line
d$ == delete to end of line

polymerase said...

Part2 will probably show how to edit with vi using your toes? Sorry for the sarcasm but why still insist on this prehistoric editor?

Any computer has arrow keys, Del/Ins, Mouse, etc. If you push to the extreme, all you need is a keyboard with two keys 0 and 1. You just need to memorize a bunch of combinations of 0 and 1 sequences and type anything you want. Is it strange that vi decides to stay in the middle, between the minimal 2 keys KB and a modern input devices.

cbleslie said...

@polymerase

Because like all things good, this is just the surface. The hotkeys are actually inteded for less keystrokes, and macros. That is to say you can automate your workflow with the VI's. You can record macros' for doing a shit tone of work in no time at all. There is a reason why people who TRUELY know vi(m) stay will vi(m).

Vi has lasted the test of time, because of it's awesome functionality. It was doing amazing things years before "wordprocessing" was ever a twinkle in many people's eyes.

http://en.wikipedia.org/wiki/Vim_(text_editor) Something you should check out.

polymerase said...

@cbleslie I have heard of VI & VIM all the time. Many persons love this editor and they cannot be all crazy. I am afraid you guys could end up making me a convert to Vi(m), I must resist. I have a mouse, it's so convenient.

Ihar Filipau said...

"Vi has lasted the test of time, because of it's awesome functionality."

It did not. Nor has it anything remotely awesome.

Fact: vi is the most hated command in all commercial UNIXes. Pretty much all admins install Emacs immediately. Or VIM.

But no sane person would ever choose to use vi.

Joe H. Rahme said...

Vi is the worst editor of all time. VIM on the other hand is the best thing to happen to Unix since the C compiler.

Okay I'll admit, I'm biased. I started learning Vim first, and every time I use Vi I really miss my Visual Blocks or Auto-completion ;-)

Anyway, nice article, thank you very much!

Ford said...

Vi is awesome. Haters gonna hate.

Suheimi said...

I am a vi user. I have tried many editors and keep coming back to vi. The reason why you use the command keys instead of arrow key and mouse is: you don't want to leave your fingers from keyboard, so that you type faster. You don't even use page up, page down key, all the cursor movement is within your finger' reach without leaving the keyboard. But this is just a surface, there many features in vi that make your typing fast. And I am still learning..

Clifford said...

I can't imagine vi is the most hated command. vi and it's variants really are awesome. Not to mention that the key bindings have almost set the standard for other cli programs. Learn vi keys and you also learn countless other programs like less and mutt. Any Linux admin worth his own weight will know vi key bindings.

Post a Comment