Linux Mall - The Linux SuperStore!
Linux books, Linux CDs, Linux toys, you name it, they have it!

W W W . L I N U X D O T . O R G

Newbie's Linux Manual
Vi Survival Guide
by Laurence Hunter
[ Home ] [ Contents ] [ Download*] [ Previous ] [ Next ]
* In Linux enter: unzip nlm.zip
Essential Learning

User-friendly it isn't, but it's guaranteed to feature on every distibution of Linux and Unix, it's the default editor for the popular e-mail client Mutt, and if you ever do a minimum install of Linux, it and Ed is all there is. Trust me, you'll opt for Vi over Ed.

There are two ways to start Vi:

  1. vi
    Open Vi with a blank unnamed document.
  2. vi filename
    Either begin editing a blank named document or open the file for editing.

Vi has 3 modes:

  1. Command Mode: Where the majority of the trickery goes on.
  2. Insert Mode: Where the majority of the typing occurs.
  3. Replace Mode: Like Insert mode, only text is overwritten.

Vi begins in Command Mode.

  • Press i from Command Mode to enter Insert Mode.
  • Press Ctrl+r from Command Mode to enter Replace Mode.
  • In either of these modes, press Esc to return to Command Mode.
Insert/Replace Mode

Type away as you would in any other text editor. The only noticeable difference is that that's all you can do, and you can't delete a line.

Command Mode

The following displays the most useful commands you can enter in Command Mode to perform a wide assortment of tasks. Note that all commands are case-sensitive.

Saving
:wWrite an already named file to disk.
:w filenameRequired to save an unnamed document. Also to save a document with a new filename.
:w! filename

Required to overwrite an existing file (other than the one you're editing of course).

Quitting
:qQuit Vi, but only if document has not been modified.
:q!Quit Vi without saving document.
:wqSave document and quit Vi.
Opening
:e filenameEither open a file for editing, or begin a new, named document.

Everything that follows, happens at the cursor. Current line means, "the line the cursor's on". Keys are indicated inside square brackets. n represents a number determined by you.

Moving
n[cursor key] Move cursor that amount in that direction e.g. 19[Down] would move cursor down 19 lines..
[PageUp]Move up 1 page. Precede with a number to move up that many pages.
[PageDn]Move down 1 page. Precede with a number to move down that many pages.
ggMove to start of document.
GMove to end of document.
:nMove to start of a specific line.
bMove to beginning of current word, or to previous punctuation mark.
nbPerform b, n times.
eMove to end of current word, or to next punctuation mark.
nePerform e, n times.
0   (zero)Move to start of current line.
$Move to end of current line.
)Move to start of next line.
(Move to start of previous line.
Undo & Redo (Both infinite)
uUndo.
[Ctrl]rRedo.
Deleting
ddDelete the current line.
nddDelete the current line + the n-1 lines below.
x or [Del]Delete current character.
Copying (Yanking) & Pasting
yy or YYank (copy) current line.
nyy or nYYank (copy) current line and n-1 lines below.
pInsert yanked text between current line and next line.
npInsert yanked text between current line and next line, n times.
PInsert yanked text between current line and previous line.
nPInsert yanked text between current line and previous line, n times.
Searching
/textFind the next instance of text from the cursor, where text is the string you're looking for. If nothing is found then Vi will wrap from the start of the document, back to the cursor.
?textThe same as above but search direction is reversed.
nNext occurence (in either direction).
NPrevious occurence.
Replacing
:s/old/newReplace next instance of old, with new. (No document wrap occurs.)
:%s/old/new/gReplace next instance of old, with new, throughout entire document.
rcharacterReplace current character with new character.
nrcharacterReplace current character + the next n-1 characters with character.
Miscellaneous
JJoin the line below, with the current line.
:helpDisplay online help.
[ Home ] [ Contents ] [ Download*] [ Previous ] [ Next ]
* In Linux enter: unzip nlm.zip
Homepage | The Last 5 Days | The Daily Linux News | The Linux Bits | Newbie's Linux Manual
The Best Linux Sites | Linux Book Reviews | A Windows Vendetta?
Diary of a Linux Newbie | Diary of an Open Source Newbie
The Linux Forum | Just For Fun
Amazon - The World's Biggest Bookstore!
4.7 million books, CDs, videos, and DVDs available to buy!
© MM Linuxdot.org | Webmaster | Manual's Copyright Terms