The vi Text Editor

(viewing version)


The vi Text Editor

Pronounced "vee-eye" ... not   "vie"   or   "vi"   or   "six"  

vi is an acronym for "visual interface"

Developed by Bill Joy in (approx) 1978 as a replacement for ed/ex

ed was an old command-line-based editor designed for Teletypes.

vi was designed to be usable over 300 baud modems   (i.e. minimal data transfer)

vi "does one thing and does it well"   (i.e. obeys the Unix tools philosophy)

vi is a text editor.


Some Facts

Performance test: changing all colons to pipes in a large password file.

Result:

emacs:  time: 25.2 real 11.5 user 1.2 sys 
vi:     time:  9.4 real  0.5 user 0.3 sys

 
"Vi is small, fast, and easy to use. Emacs is huge, slow and easy to use"
- Scott McMahon, softbase@mercury.interpath.net, 1998

 
"Which editor was used to write the first version of emacs?"
- Me, jas@cse.unsw.edu.au, August 2000

 
vi is good for 1/2-fingered typists; no "chords" required.


vi "Features"

vi has "modes" ... vi essentially deals with one file at a time


Why you must learn vi

It is (notionally) the default editor on Unix-based systems.

Many Unix applications that need to invoke an editor, invoke vi.

The EDITOR environment variable may save you ...

But some applications ignore even that and suddenly ... you're in vi!


vi Survival Skills

The two most important things to remember:


Modes

Command mode: Insert mode: ex mode:


Movement Commands

A range of movement commands are available, including ...

h j k l left, down, up, right   (obvious!)
w b e forward / backward / move.to.end.of one "word"
^ 0 $ first.non.blank / beginning / end of current line
} { forward / backward by one "block"
^F ^B forward / backward by one "page"
G go to end of buffer
ddd G go to line ddd

All movements can be preceded by a count   (e.g. 10w = forward ten words)


... Movement Commands

Movement can also be achieved by searching ...

/ pattern move to next occurrence of pattern in file
? pattern move to prev occurrence of pattern in file
f c move to next occurrence of c in current line
F c move to prev occurrence of c in current line


Change Commands

Insertion commands enter Insert Mode; delete simply removes text ...

x delete character under cursor
i insert text immediately before cursor
a append text immediately after cursor
o open a new line below current line
O open a new line above current line
s subsitute text for character at cursor
S subsitute text for whole of current line


... Change Commands

Some change commands act on a region of text, specified by movement ...

d move delete text from old to new cursor positions
c move change text from old to new cursor positions
! move cmd filter text from old to new cursor through cmd
< > move left / right shift text from old to new cursor
y move grab a copy of text from old to new cursor posn

Doubling a change command (e.g. dd) means "do it to the whole line".

Changed text is placed into a buffer, from where it can be retrieved.


... Change Commands

Examples:

dwdelete to start of next word
d0delete to start of line
dHdelete to top of page
d}delete to end of "block"
d/abcdelete up to next abc
dddelete current line
ccchange current line (like S)
!}fmtformat a paragraph


Ooops

Several levels of undoing are available ...

u undo last change
U undo all recent changes to current line
:e! quit from vi and abandon all changes
:e! undo all changes to file and start again

Later versions of vi support multiple u undo's.


... Ooops

Not really "ooops", but you can retrieve a copy of changed/yanked text

p paste saved text after cursor
P paste saved text before cursor

Precise paste effect depends on whether it was part or multiple lines saved.


Repeating Yourself

You can repeat the previous change command via .

Leads to an idiom for skipping through file making similar changes:

Of course, if you really want to make a global subsitution:

:%s/pattern/replacement/g


Wait! ... there's more ...

Along with all this ... vi also provides: For further details:


Produced: 10 Aug 2000