Website based on my experience working as System Administrator

LINUX INFORMATION WEBSITE

This site is dedicated to providing tutorials, help, guides and links for Linux users.
HOME PAGE
LINUX COMMANDS
   
   
   
   
   
   
   
   
   

LINUX COMMANDS

Contents | Previous | Next

Vi survival guide

Vi is the popular text editor guaranteed to feature on all Linux and Unix distributions, and is ideal for quickly editing configuration files from the command-line.

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 text editor.

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.

Save

:w Write an already named file to disk.
:w filename Required to save an unnamed document, or save a document with a new filename.
:w! filename Required to overwrite an existing file (other than the one you're editing).
 

Quit

:q Quit Vi, but only if document has not been modified.
:q! Quit Vi without saving document.
:wq Save document and quit Vi.
 

Open

:e filename Either open a file for editing, or begin a new, named document.
:r filename Add contents of file filename between current line and next line.
 
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.
 

Move

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.

gg Move to start of document.
G Move to end of document.
:n Move to start of a specific line.

b Move to beginning of current word, or to previous punctuation mark.
nb Perform b, n times.
e Move to end of current word, or to next punctuation mark.
ne Perform 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 and redo

u Undo.
[Ctrl]r Redo.
 

Delete (cut)

dd Delete (cut) current line.
ndd Delete (cut) current line plus the n-1 lines below.
x or [Del] Delete (cut) current character.
 

Yank (copy) and paste

yy or Y Yank (copy) current line.
nyy or nY Yank (copy) current line and n-1 lines below.

p Paste text between current line and next line.
np Paste text between current line and next line, n times.
P Paste text between current line and previous line.
nP Paste text between current line and previous line, n times.
 

Search

/text Find 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.
?text The same as above but search direction is reversed.
n Next occurence (in either direction).
N Previous occurence.
 

Replace

:s/old/new Replace next instance of old, with new. (No document wrap occurs.)
:s/old/new/g Replace all instances of old, with new.
rcharacter Replace current character with character.
nrcharacter Replace current character plus the next n-1 characters with character.
 

Miscellaneous

J Join the line below, with the current line.
:help Display online help.

Contents | Previous | Next

 
Last Update: Jan 2003

HOME | CONTACT | FAQ'S | TRICKS & TIPS | WEIRD NEWS
This Material has been taken from different sources. Its free for anyone to use and reproduce.