vi-Commands

You can get a listing of vi commands by typing :viusage within the vi.

Variables concerning the vi editor can be set and unset by

     :set name [=value]
     :set no [name]
     (for example: :set wrapmargin=2 and :set wrapscan to set auto line wrap)

  
within the vi editor or in ~/.exrc file, or by setting the EXINIT variable.

The vi editor has 2 different modes:
The command and the insert mode respectively.
You can enter the insert mode by the text insertation commands and you can leave the insert mode with <Esc>.

You can repeat the last vi command by . and you can undo the last command by u

Screen Control Commands


<CTRL>-l reprint current screen
<CTRL>-y expose one more line at top of screen
<CTRL>-e expose one more line at bottom of screen

Paging Commands


<CTRL>-f page forward one screen
<CTRL>-b page back one screen
<CTRL>-d page down half screen
<CTRL>-u page up half screen

Cursor Positioning Commands


<CTRL>-g return the current line number
% goto matching parenthesis
j or <CRTL>-n move cursor down one line, same column
k or <CTRL>-p up one line, same column
h back one character
l or <Space> forward one character
- to beginning of last line
0 or ^ to beginning of current line
+ or <Return> to beginning of next line
nG to beginning of line n. Default is last line of file
'[a-z] jump to mark [a-z]
'' jump back to previous location
$ to end of current line
e to end of current word
w to beginning of next word
b to beginning of last word in current line
H to first line of current page
L to last line of current page
M to middle of current page

Text Search Commands


/pattern move cursor forward to next occurrence of pattern
?pattern move cursor backward to next occurrence of pattern
n repeat last / or ?pattern search
N repeat last / or ?pattern search in opposite direction
:nu give current line number

Text Insertion Commands


a append text behind cursor
A append text at the end of the line
i insert text before cursor
I insert text at the beginning of the line
o open new line below the current line for text insertion
O open new line above the current line for text insertion
<Delete> overwrite last character during text insertion
<Esc> stop text insertion

Many of the following commands have the format:

command - repeat factor - position

Text Deletion Commands


d0 delete to beginning of line
db delete to beginning of word
dnw delete to end of (nth) word (Almost every command can be given with a repeat factor n)
dnB delete to beginning of (nth previous blank) delimited word
dL delete through last line on screen
dH delete through first line on screen
dG delete until last line of file
d1G delete until first line of file
dW delete to end of blank delimited word
dn) delete to end of (nth) sentence
d( delete to beginning of sentence
d} delete to end of paragraph
dn{ delete to beginning of (nth preceding) paragraph
ndd delete n lines beginning with current line. Giving no number n deletes only current line.
D or d$ delete from cursor to end of line
P, p put back text from the previous delete or yank command before/behind cursor
x delete current character

Text Change Commands


rx replace current character with x
R replace remaining text until stopped by pressing the <Esc>
s change current character
S change current line
cw change characters of current word until stopped by pressing <Esc>
C or c$ change remaining text on current line until stopped with <Esc>
~ change case of current character (upper case - lower case)
px transpose current and following characters
J join current line with next line
:n,m s/old/new/[cg] change (within the lines n to m) the string old to new (confirm/ globally)
:g/old/s//new/[cg] replace all occurences of string old with new

Buffer Usage Commands


"[a-z]nyy yank (=copy) n lines to the [a-z] buffer. Default is current line
"[a-z]np put n yanked text lines from the [a-z] buffer behind the cursor
m[a-z] mark cursor position with a letter
"[a-z]y'[a-z] yank the lines to the [a-z] mark to the [a-z] buffer

Exiting vi


ZZ exit vi and saves changes
:e name edit the file name without quitting vi; the buffers are not changed, so text can be copied from one file to another
:n edit the next file from the command line
:q quit edit session (no changes made)
:wq write changes to current file and quits edit session
:w name write to the file name
:w >> name append the buffer to the file name
:n,m w name write the lines n to m to the file name
! all these commands can be forced by !
(for example: :w!)

Options


+cmd tell the editor to begin by executing the specified command cmd.
A useful example would be +/pattern to search for a pattern
-l set the showmatch and lisp options for editing LISP code
-r name retrieve the last saved version of the name'd file (in the event of an editor or system crash).
If no file is specified, a list of saved files is produced
-w n set the default window size to n.
This option is useful for starting in a small window on dialups
-x cause vi to prompt for a key.
The key is used to encrypt and decrypt the contents of the file. If the file contents have been encrypted with one key, you must use the same key to decrypt the file

Examples

Text Searching


CommandResult
/and find the next occurrence of the string and
Examples: sand, and, standard, slander, andiron
/\<and\> find the next occurrence of the word and
Example: and
/^The find the next line that starts with The
Examples:
The UNIX operating system ...
There is no ...
/^[0-9][0-9]) find the next line that starts with a two-digit number followed by a right parenthesis
Examples:
77)...
01)...
15)...
/\<[adr] find the next word that starts with an a, d, or r
Examples: apple, drive, road, argument, right

Text Substitution


CommandResult
:s/bigger/biggest replace the string bigger on the current line with biggest
Example: bigger -> biggest
:1,.s/Ch 1 /Ch 2/g replace every occurrence of the string Ch 1, before or on the current line, with Ch 2
Examples: Ch 1 -> Ch 2, Ch 12 -> Ch 22
:1,$s/ten/10/g replace every occurrence of the string ten by the string 10
Examples: ten -> 10, often -> of10, tenant -> 10ant
:1,$s/\<ten\>/10/g replace every occurrence of the word ten by the string 10
Examples: ten -> 10
:.,.+10s/every/each/g replace every occurrence of the string every by the string each on the current line through the tenth following line
Examples: every -> each, everything -> eachthing

Buffer Usage


CommandResult
ma mark cursor position with letter a
d'a delete text up/down to mark a (and put it into buffer)
p insert last buffer (named or not) behind cursor position
mf mark cursor position with letter f
"hy'f yank text to mark f into buffer h
"hP insert buffer h before cursor position