Dennis Cook
This is roughly the fourth version of a series of increasingly improbable guides to vi, the first two being entitled ``Vi--A Quick and Dirty Guide''; the third, ``Vi--A Quick and Nasty Guide.'' Constructive criticism, or more likely blame, can't be e-mailed to dennis@socs.uts.edu.AU --- not only has Dennis left the building, but the building has moved. If you must talk to somebody about problems with this chapter, try wzdd@progsoc.uts.edu.au. Dennis would like to thank Brent Curtis and David Morgan for advice on LATEX, and Grant Heinrich for his merciless, but much needed, editing.
Vi, pronounced ``Vee-Eye'', is a full screen text editor traditionally associated with Unix. Like most Unix tools, it is characterised by being useful and powerful when mastered, but also has an atrociously high learning curve, an interface that is anything but intuitive, and all the charm and character of the lukewarm brown liquid that passes for coffee29.1in the Broadway cafeteria.
Vi has been around for along time, and has splintered into about a million different versions. vim is a very popular incarnation, but you might also want to check out gvim (it's graphical!), nvi and elvis. All of these build upon basic vi.
Having read thus far, one might be wary of this editor. Overhearing the first year students cursing its very name, one might be inclined to think twice and use some more ``friendly'' editor like aXe.
There are a couple of reasons why you might wish to persevere. Firstly, vi is standard throughout almost all Unix systems. No matter what system you use, you will always have vi and ex; the same cannot be said for aXe or emacs. Vi can also run on PCs and Macintoshes.
The second reason is a little more complex, and has to do with the style of the user interface. Vi works on the principle that although difficult to begin with, it allows an experienced user to work far more efficiently than they would with a friendlier but more cumbersome interface29.2.
So, in summary, if you plan to do a lot of editing29.3or you want to be able to use a text editor no matter what Unix system you're using, read on.
(You won't be thought any the worse if you wimp out and use aXe, but remember, at least vi has an undo command, and can recover your file when the system crashes in the middle of editing!)
In the examples,
things that you type will normally be set in courier,
but things that you have to fill in, such as filenames,
will be set in italics.
(Optional values will be given in parentheses29.4.)
Responses that the machine gives will generally be in boldface.
This
A complete summary of all the vi commands known to man is given
at the end of this chapter; this should come in useful as a reference.
The basic command line for vi is
Very occasionally, you may have to use the -r option.
This only happens if
the system crashes29.5 or
your session or window unexpectedly dies while you're in Vi.
Your file will be saved, and you will get some
mail, telling you that you can recover the file using the
command
When you're in vi, the editor makes a copy of your
file into a buffer, which is what you actually edit.
When you finish a session, you must decide whether you wish
to save the contents of the buffer to the file,
or whether you wish to get out without making any
permanent changes.29.6
When you exit vi, first press
ESCAPE to get into command mode. (If it beeps then
you're already in command mode29.7.) You now have two options:
whether you wish to write the buffer to the file
you're editing, or quit without writing.
At this point you may get the message,
No write since last change, :quit! overrides.
This is vi warning
you that you've made changes, and haven't saved them. If you
wish to exit without making changes, then follow the
instructions and type :quit!
A quicker way to exit vi,
is to type :x
It reads your TERM environment variable to determine
what type of terminal you're using;
if this has been incorrectly set vi will not be able to function.
Also check your stty settings to ensure the correct size
of your terminal (in terms of lines and columns) has been recorded.
This should be done automatically, but the system may lose track.
Try not to resize the Vi window when you are in vi---some versions don't
notice, and the results are ugly.
Vi also looks for a file called .exrc in your home directory
and reads any commands there,
and also from your EXINIT environment variable.
You use these to configure vi,
by placing commands from last-line mode in them.
A typical .exrc file might read:
This tells vi to display a message,
telling you which mode it's in;
to switch on auto-indentation mode;
and to show matching pairs of braces and parentheses.
Note that the commands are not prefixed with a colon.
This section gives an overview of how vi works,
and how it differs from some other text editors.
After reading this section through,
you should consider experimenting with some
of the commands described here.
The principal difference between vi and some other editors
is that vi is what's known as a modal editor.
Many editors work by having the user type in text,
which is inserted at the cursor,
while other keys,
such as control and function keys,
are used to carry out changes,
such as deletion, substitution,
and moving the cursor.
Vi, however, operates in two quite different modes:
input mode, where characters that are typed are
inserted at the cursor position, and
command mode, where characters typed have a special
meaning to the editor. (e.g. a lowercase x tells the
editor to delete one character to the right of the cursor.)
Vi typically starts up in command mode,
so the user normally enters a command,
such as i for insert,
to switch to input mode.
When finished, the user switches back into command mode
by pressing the ESCAPE key.
There is an additional mode,
called ``last-line'' or Ex29.8mode, for giving complicated commands.
When you start up vi the contents of your
file (if it exists) will be
displayed on the screen,
along with a message on the bottom line
telling you how large the file is.
If your file fits completely on the screen,
The bottom line of the screen is used for vi to display
warning and error messages,
general information,
and to type in commands in last-line mode.
To add text to the file, you'll need to get into input mode.
There are four basic commands to do this:
append after cursor,
append to end of line,
insert before cursor,
insert at beginning of line, and
open a new line.
These commands are given as the keystrokes
a,
A,
i,
I, and
o or O.
These commands are case sensitive;
a does not have the same effect as A.
The input commands are listed in Table 4.5.
For example, assuming you have an empty file,
you could start entering text by typing i for insert.
Any text that you type will now appear on the screen,
until you press ESCAPE.
You can correct mistakes on a line by using either
the BACKSPACE or DELETE keys to delete
a single character,
CTRLW to delete words,
and CTRLU to delete lines29.10.
When you press RETURN (
You'll have already noticed that there is
no readily apparent
way of discerning whether you're in command mode or
input mode, without experimentally pressing a few keys
to see what happens.
A way around this is to make vi show you what mode it's
operating in.
Do this by going to command mode --- press ESCAPE: if you're
in command mode already you'll get a beep,
if you're in input mode you'll be transferred to command mode.
Then type :set showmode
This setting will remain in effect only for your current
session in vi; to make it permanent,
you will have to put an entry for it in your .exrc file.
In each case, you return to input mode by pressing
the ESCAPE key.
Note how most of the key strokes are a one letter mnemonic
for their function,
that they are case sensitive,
and that a command in upper case is usually a variant
of one in lower case.
In command mode commands are either motion commands,
for moving around the file,
or commands that change (or delete) text.
Motion commands can be given in terms of characters,
words, lines, sentences, paragraphs, pages, or search patterns.
Motion commands are typically a single character,
optionally prefixed by a number,
indicating how many times to repeat it.
For example, typing 10
Also worth mentioning are the caret and dollar-sign keys,
Since movement by character is also the most tedious way
of moving around the file,
you may wish to read on for more elaborate ways of finding your
way around the file.
Another important command is G, or goto line.
If you type an upper case G on its own,
the cursor will move to the end of the file.
However, if you precede the G with a number n,
the cursor will move to the start of line n.
For example, typing 55G will move the cursor
to the start of line 55;
typing 1G will move the cursor to the start of
the file, i.e. line 1.
There are two other commands,
CTRLY and CTRLE which cause
the screen to scroll up and down by one line,
but leaving the cursor in the same place.
Also, you can position the cursor on the
first, middle or last line of the screen
by typing H, M, or L respectively.
(The mnemonics for these are Home, Middle, and Last.)
/pattern
where the pattern is a string of characters.
Note that when you type the slash,
the cursor moves to the last line of the screen,
and that you terminate the pattern by pressing RETURN.
The simplest pattern contains just the characters you're
searching for.
For example /dog
This command searches forwards through the file;
to search backwards, replace the slash with a question mark,
e.g. ?dog
Once you've entered a pattern, you can search for the next pattern
like it in the same direction by typing n,
for next.
The direction of the search is reversed by typing~N.
Change commands are used to delete and replace specific items of text,
and have a slightly more complicated syntax than the movement commands.
The other replace command,
R, acts as an ``overtype'' command;
each character you type will replace the current
character and move the cursor to the right
until ESCAPE is pressed.
For example, if you place the cursor at the
start of the word ``internal'',
and typed the sequence, "Rex
The preceding number is optional.
When a command of this type is issued,
the editor marks the area you are about to
change with a dollar sign.
The replacement text is typed in,
and when ESCAPE is pressed,
any remaining text of the old unit will be gobbled up.
An example of this would be the change word command,
or cw.
Typing cw would cause the last character of the
current word to be replaced by a dollar sign;
any text entered until you press ESCAPE would replace that word.
There are three other change commands: C,
which changes to the end of the current line29.11,
cc which changes the entire current line,
and s, which deletes one character to the right,
then starts inserting29.12.
Some examples are listed in table 4.2.
Vi also provides an undo command.
There are two of these, u and U.
The first variety, u undoes the last command.
Note that after typing u once,
a second u will result in the first undo being undone.
If you make a number of changes to a particular line,
you can recover the original line by typing U.
This will recover the line as it was before you
positioned the cursor on it.
Obviously if you move the cursor off a line after making
a number of changes,
you can't then move it back and type U,
since it's no longer the current line.
(number)dmovement command
where d stands for ``delete'',
the number is optional,
and the motion command specifies the scope.
For example, dw will delete to the end of
the current word, and 6dw will delete 6 words29.13.
Other deletion commands are dd to delete one line,
ndd to delete n lines,
and D to delete from the cursor to the end of line.
There are also two shortcuts: x, which deletes a single
character to the right of the cursor,
and X, which deletes a single character to the
left of the cursor.
Some examples are shown in table 9.1.
The yank command functions in a similar manner to the
change and delete commands, it has the form of
(count)ymovement
with the usual variations.
Some of these are y$, to yank from the cursor to the end of line;
yy, or Y to yank the current line;
10yy, to yank the next 10 lines;
y), to yank to the end of the current sentence.
There are two variations of the put command,
p and P.
The first of these, p copies out the contents of
the buffer after the cursor.
If the buffer contains a whole line,
or more, its contents will be copied out onto the next line.
The uppercase P dumps
the buffer before the current character or line.
A useful application
of this is using the sequence xp to transpose
two adjacent characters,
and the sequence ddp to transpose two adjacent lines.
"acommand
where a is a single lower case character,
naming the buffer,
and command is either a delete or yank.
For instance, to delete 3 lines into buffer ``q'',
type "q3dd.
This buffer could then be recovered at any time
in the same session by typing "qP.
If you specify the buffer name in upper case
in a delete or yank operation,
the text will be appended to that buffer.
For example, "Q3dd will delete 3 lines,
and append them to whatever is already in buffer q.
While these are buffers are not preserved
when you quit vi,
they are preserved when you change files
with the :e command.
The shell command has the form of either
!movement command shell command
For example, to sort the first twenty lines in
the file into alphabetical order,
move to the top of the file (1G) and
type 20!!sort
The commands issued in last line mode can be divided into
three main groups.
These commands all start with a colon, and are entered on the
last line of the screen.
You can edit them using backspace, CTRLU and CTRLW,
and execute them by pressing
The commands in this section are used for reading files into
the buffer, writing the buffer to files, changing from one
file to another, and exiting vi.
In either case, when you use the :e command,
and you've modified the current file,
vi will refuse to let you change files unless
you either write the current file (i.e. :w
If you've specified multiple files on the command line,
you can change files by typing :n
To quit, type :q
A shortcut for :x
There are various options that the user can set
to govern the way vi displays, and inputs text.
One example of this was the showmode
option discussed
The command to access these options is :set,
and is used in last line mode.
Typing :set
There are two types of option, those
which are toggles, and can be either on or off,
and those which take a numeric value.
To set a toggle or boolean option, type :set option
With numeric options, the form of the command is
:set option=n,
where n is a number.
For example to set the word wrapping margin (q.v.) to 20,
type :set wrapmargin=20
While covering all of the options available is beyond
the scope of this guide29.14,
a few of the most useful options will be described here.
For example, if you type in a line starting with 3 spaces,
and press RETURN,
the cursor will automatically start in the fourth column,
underneath the first character of the previous line.
In order to move the cursor left at this point,
use CTRLD.
It is preferable to use the TAB key to indent,
as vi will automatically replace sequences of more than
8 spaces at the start of a line with TAB characters.
To use increments of indentation other than TAB characters,
set the shiftwidth (abbreviated sw) parameter
to the number of spaces you require.
For example, many people prefer to indent their program
code in increments of four spaces.
To do this, type :set shiftwidth=4
Note that the tabstop and hardtabs options
are used for controlling how vi displays TAB characters
on your terminal; they are not normally altered.
Since a TAB character is just a CTRLI,
any instances are represented by the sequence ^ I.
Some readers will also be familiar with a
UNIX editor called ex, which is a line, rather than
screen-based editor. It is useful then to know that your
efforts in learning ex have not been in vain.
Indeed,
in the last section you will notice that some of the
commands are suspiciously familiar. This is because ex and vi are essentially two different sides of the same
program.
You can switch from ex to vi while editing a file
simply by typing vi at the colon (:) prompt in
ex. Likewise, you can go from vi to ex by
typing Q, while in command mode. You can also enter an
ex command whilst in vi simply by preceding it
with a colon.
Occasionally, when vi suffers a particularly nasty error,
it will change into ex29.15.
Just type vi
Another way
of changing your current file is doing so by tag.
When you type the command ``:ta tagname
abbr uts University of Technology, Sydney
in your .exrc file.
Now, whenever you type uts in input mode,
followed by a non alphanumeric character,
such as a space,
it will be replaced by the full name.
To remove an abbreviation,
type :unabbr lhs
The :map command has the format of
:map lhs rhs
where lhs is the keystroke(s) that you want replaced
by rhs.
For example, if you found you needed to transpose characters
a lot, you could type :map T xp
More useful however, is the :map! command,
which behaves exactly the same way in input mode.
One of the most useful applications of this is to
re-program the arrow keys so they don't stuff up
your file.
To re-program the up arrow key to leave input mode,
and then move up a line,
you'd use the sequence
Note that you'll probably need to precede the up arrow key with
a CTRLV,
and also the ESCAPE key.
(The k tells vi to move the cursor up a line.)
A search and replace is performed when you want to change
some or all occurrences of a particular pattern of characters
in your file to something else. For example, in a Pascal
program you may want to change all occurrences of the
string integer to real and so on.
The search and replace commands are the hardest to
master and almost impossible to remember.
You will just have to practice these until you get
confident about them.
In order to replace a pattern you
The expression /^ procedure/ would find the word
``procedure'' only if it were at the start of a line.
The dollar-sign operates in a similar manner, and the two could
be combined: e.g. the pattern /^ end$/ would find lines
whose sole occupant was the word ``end''.
A caret after the left bracket has the effect of negating
the list or range of characters.
The expression /c[^ au]t/ would match any three letter
group starting with ``c'', ending with ``t'',
but not having ``a'' or ``u'' as the middle letter.
where regexp is a regular expression as described above,
and string is the set of characters to replace it with.
Without any scope specification,
this will substitute the string for
the first occurrence of regexp,
on the current line.
For example, typing :s/dog/cat/
To have vi ask for confirmation before substituting,
add a c at the end, e.g. :s/dog/cat/gc
Once you've performed a substitution,
typing an ampersand in command mode
will cause the substitution to
be repeated on the current line.
The s can be prefixed with a line number range,
for example, the command :1,30s/dog/cat/g tells
the editor to replace all occurrences of ``dog'' with ``cat''
on lines 1 to 30. To do a global substitution, you could use
:1,$29.18,
or :g, or even :
One of the best things about the substitution syntax is that various other
shell tools, such as sed, have adopted it. Not exactly, of course,
because that would be too easy, but you're sure to find it hauntingly familiar.
where regexp1 and regexp2 are regular expressions.
The :g could also be written as :
:
Note that you still have to use the scope at the end of the line.
What a command of this form says to do is:
The :g could also be written as :
:
Note that you still have to use the scope at the end of the
line29.19.
Sound confusing? The best way to learn this is by example.
Take a look at and try out some of the examples below.
Below are some typical examples of substitution commands.
There are a few other significant characters that you
can put in regular expressions in vi.
Instead of being nullified by the backslash character,
they are introduced by the backslash!
They are
For example, the pattern
Approach
The next two sections contain instructions on how to enter and leave vi,
followed by an outline of its basic capabilities.
At the very least, you should read these two sections through,
then re-read them while experimenting with the commands on a file.
After that, you should become familiar with the last-line commands
in the next section, and the search and replace commands which
are given in a section of their own.
Esoterica
Finally, after you have become a bit more confident with the editor,
you should look at the sections entitled ``Esoterica'',
which follow each principal section. These contain extra information,
shortcuts and dirty tricks you may wish to try out.
Getting into (and out of) Vi
vi filename
where filename is the name of the file you wish to edit.
vi -r filename
. Backups are not performed continuously, so you may lose up to the last ten minutes or so of your session.
Esoterica
There are a number of factors that affect the way vi will
behave when you start it up.
Starting to Learn Vi
Modes
Screen Layout
Adding Text
Moving Around
Moving by Character
The simplest way of moving around the file is character by character.
As well as the arrow keys,
the keys h, j, k and l,
move the cursor left, down, up and right respectively.
If a movement command cannot be carried out,
for instance moving right when the cursor is at the
end of the line, vi will beep.
Moving by Line
The simplest way of moving forward a line is to press the
RETURN key.
If a new line opens up when you do this,
you're still in input mode.
Other ways include the
Moving by Screen
There are two ways of moving through a file by screen:
scrolling and paging.
The scrolling commands are CTRLU and
CTRLD which scroll up and down half
a screen respectively.
The paging commands are CTRLB and
CTRLF which page backward and forward
by a whole screen at a time.
Words, Sentences, and Paragraphs
Vi also allows movement in terms of words,
sentences and paragraphs.
Vi regards a word as
a series of alphanumeric characters,
separated by spaces and punctuation marks.
Sentences are terminated by a full-stop,
question or exclamation mark,
followed by a newline or two spaces.
The motion commands are shown in table 14.6.
e
Move to the end of the current word
b
Move to the beginning of the current word
w
Move to the start of the next word
)
Move to end of current sentence
(
Move to beginning of current sentence
}
Move to end of current paragraph
{
Move to beginning of current paragraph
]]
Move to end of section
[[
Move to start of section
Searching
You can position the cursor by searching for a pattern.
The form of this command is
Changing Text
The Replace Command
The replace commands allow you to replace a single character
or overtype a line of text.
To replace a single character,
move the cursor onto that character and type rc,
where c is the character you wish to replace it with.
Note that you don't have to press ESCAPE when done.
The Change Command
A change command allows the user to select and replace
a specific unit of the text.
The basic form of a change command is
(number)cmovement command
c3w
Change three words.
Change from the cursor to end of the third word
on the right.
c$ or C
Change to the end of line.
Changes everything between the cursor and the
end of line.
c3
Change three lines.
c)
Change to end of sentence.
cl or s
Change one character to the right of the cursor.
The Undo Command
Deleting Text
Now for some real fun.
The delete commands work in much
the same way as the change commands.
Most commands take the form of
d3w
Delete three words.
Delete from the cursor to end of the third word
on the right.
d$
Delete to the end of line.
Delete everything between the cursor and the
end of line.
d^
Delete to the start of the line.
d3
Delete three lines.
d)
Delete to end of sentence.
dG
Delete from the current line to the end
of the file.
d1G
Delete from the current line to the
beginning of the file.
dl or x
Delete one character to the
right of the cursor.
3X
Delete three characters to the
left of the cursor.
Yank and Put
When anything is deleted,
it is placed in a buffer from which it can be recovered,
either by the undo command,
or by the put command.
This process is sometimes referred to as cutting,
copying and pasting.
Material can also be put into this buffer
without deleting by using the yank command.
Other Stuff
There are some other commands that don't fit in anywhere else.
``"starting.tex" [Modified] line 656 of 688 --95
Esoterica
You'll have
probably noticed the mess made when you try to press
an arrow key while in input mode.
This is because an arrow key just transmits a normal sequence
of characters which are interpreted by vi.
For example, on a VT100 terminal, or an xterm window,
the
Occasionally
you'll need to insert control characters into your file
which have a special meaning to vi,
such as ESCAPE and RETURN.
In order to do this, precede them with a CTRLV.
Naturally, a CTRLV in your file is produced by
typing two CTRLV's in a row.
As well as
using just plain yank and put commands with a single buffer,
you can yank and delete text into explicitly named buffers for
retrieval later.
The form of these commands is
Vi also puts anything deleted into numbered buffers:
buffer 1 holds the item most recently deleted,
buffer 2 the second most recent item to be deleted,
and so forth up to buffer number 9.
These buffers are retrieved in the same way as
the alphabetic buffers,
for example, "1p inserts the item most
recently deleted after the cursor.
The sequence, "1pu.u.u.u. etc. will
recover up to the last nine deletions in reverse order.
Another interesting, although rarely used feature is
the capability of passing certain portions of the file
through a Unix command.
For example, you might want to sort the first 20 lines
of the file into alphabetical order.
Options and Last-Line Mode
Manipulating Files
Reading Files with :r
The :r command inserts the contents of a named file
into the buffer at the current line.
For example, to read in a file called ``thang'',
type :r thang
Writing Files with :w
The :w command writes the buffer to either the current
file or another file.
To write the current file, just typed :w
Changing Files (:e and :n)
It is possible to select another file to edit from
within vi this is done with the :e or :edit command.
If you don't specify a file, by typing :e
Exiting Vi
There are a number of ways of exiting vi.
These were also discussed in Section 3.1,
but are included here for completeness.
Setting and Displaying Options
Some Commonly Used Options
autoindent
Auto indentation allows you to enter text
number
This option causes vi to display a line number to the left
of each line.
Note that the line numbers are not physically part of their lines;
the editing window is effectively shifted to the right
to accommodate them.
To switch on, type :set number
list
Typing :set list
showmode
The showmode toggle discussed earlier,
causes vi to place a message in the bottom right hand
corner of the screen indicating whether it's in
in input or command mode.
showmatch
When activated, this toggle causes vi to show matching
braces and parentheses when in input mode.
When a right brace or parenthesis is typed,
and the matching left one is on the screen,
the cursor will briefly flash on the matching item.
If an unmatched right brace or parenthesis is typed,
vi will beep in warning.
Vi and Ex
Esoterica
A way of making input
less painful is the :abbr or
abbreviate command.
This has the format of :abbr lhs rhs
Vi also has a
parameterless macro facility which allows the user to assign
functions to particular key sequences in command mode,
using the :map command,
and likewise in input mode,
using the :map! command.
:map!
Search and Replace
Seek and Destroy
Basic Regular Expressions
Substitution Commands
Line-Based Substitutions
There are a number of flavours of substitute command.
The first variety takes the form of
:s/regexp/string/scope
Global Substitutions
The next variety, instead of operating on the current line,
searches for the next instance of the pattern.
It takes the form
:(g)/regexp1/s/regexp2/string/(scope)
Examples
Search for the next occurrence of the word ``mushroom''
and replace it with ``fungus''.
Search the entire file and replace the first
occurrence of the word ``procedure'' on each line with
word ``function''.
Search the entire file replacing every occurrence on
every line of ``cat'' with ``dog''.
This would replace every string between parentheses
with nothing, effectively deleting it. Remember the
significance of the asterisk, applied to the
period: i.e. ``zero or more occurrences of
any character.''
Trim all trailing blanks of the end of every line in the
buffer.
Trim all leading blanks from every line in the buffer.
A slight variation. The d tells vi, to delete
the line. Therefore, this has the effect of deleting any
line with the word ``excrement'' in it. Do not,
under any circumstances try :g/.*/d. (See if you can
work out what it does, though).
Replace all expressions ``3 * 2'' with ``3 + 2''. Note the use
of the backslash to make vi ignore the special meaning
of the asterisk.
Replace all occurrences of ``vi'' with ``Vi'', but asking for
confirmation each time. (Type a ``y'' to change, an ``n'' not to
change.)
Esoterica
If you thought the other sections of esoterica were bad,
then you ain't seen nuthin' yet.
The other interesting character is the ampersand.
When used in the replacement string,
it represents the original regular expression.
For example, the command :g/th[ai]ng/s//
If you still don't feel satisfied,
some even more twisted variants on regular expressions
and replacement strings can be found in.
Vi Command Summary
The following tables contain a summary of all the commonly
used commands for vi and ex. They include the
commands already discussed, as well as further commands.
Feel free to experiment.
Invoking vi
vi file
Edit at first line of file.
view file
Edit in read-only mode.
vi file1 file2
Edit multiple files via :n.
vi [options] files
As above, with options.
Command~Line Options
+n
Start editing at line n.
+
Start editing at last line.
+/pattern
Start at pattern.
-R
Open file as read only.
-r
Attempt file recovery after crash.
-wn
Specify window size of n lines.
Exiting vi
ZZ
Exit and save changes, if any.
:x
As above.
:wq
Write changes, then quit.
:w
Write changes to original file.
:w>>file
Append buffer to file.
:m,nwfile
Write lines m to n to file.
Text Input
i
Insert at character before cursor.
I
Insert before first non-blank character on line.
a
Append after cursor.
A
Append at end of line.
o
Open and insert at line below.
O
Open and insert at line above.
Input mode is terminated by pressing ESCAPE.
Input Mode Commands
CTRLW
Delete word.
CTRLH
Delete last character.
CTRLU
Erase current line.
CTRLD
Backspace over autoindent.
CTRLT
Insert soft TAB in autoindent.
Screen Adjustment
CTRLL
Redraw screen.
CTRLE
Scroll up one line.
CTRLY
Scroll down one line.
CTRLB
Page backwards.
CTRLF
Page forwards.
CTRLD
Scroll down.
CTRLU
Scroll up.
Cursor Motion
H
Move cursor to top of page.
M
...to the middle of page.
L
...to last line of page.
l
Cursor left.
h
Cursor right.
k
Cursor up.
j
Cursor down.
^
Move to beginning of current line.
$
Move to end of current line.
n
Move to column n.
tc
Move to character c.
w
Forward one word.
W
Forward one word including punctuation.
b
Backward one word.
B
Ditto inlcuding punctuation.
e
Move to end of word.
E
Ditto inlcuding punctuation.
File Motion
G
Last line of file.
nG
Line n of file.
/pattern
Next line containing pattern.
?pattern
Previous line containing pattern.
n
Next ? or /.
N
Last ? or /.
)
Next sentence.
(
Last sentence.
}
Next paragraph.
{
Last paragraph.
]]
Next section/function.
[[
Last section/function.
Change
cw
Change to end of word.
ncw
Change n words.
C
Change from cursor to end of line.
c^
Change from cursor to start of line.
cc
Change line.
ncc
Change n lines.
c/regexp
Change from cursor to pattern.
c)
Change to end of sentence.
c{
Change to beginning of paragraph.
c}
Change to end of paragraph.
s
Deletes the character to the right of
the cursor and enters input mode.
Yank and Put
yy or Y
Yank current line into buffer.
nyy or nY
Yank n lines into buffer.
p
Put buffer after cursor.
P
Put buffer before cursor.
"ay
Yank into buffer a where a is a lowercase
letter, i.e. a...z.
"Ay
Append into buffer a.
"ap
Put from buffer a.
"np
Yank from buffer n, range 1...9.
Deletion
x
Delete to the right of the cursor.
nx
Delete n characters to the right of the cursor.
X
Delete to the left of the cursor.
nX
Delete n characters to the left of the cursor.
dw
Delete to end of word.
ndw
Delete n words.
D
Delete from cursor to end of line.
dd
Delete line.
ndd
Delete n lines.
d/regexp
Delete from cursor to pattern.
d(
Delete to beginning of sentence.
d)
Delete to end of sentence.
d{
Delete to beginning of paragraph.
d}
Delete to end of paragraph.
"ad
Delete into buffer a.
Replace
r
Replace single character.
R
Overtype until ESCAPE pressed.
Setting Options
:set
Shows current settings of most options.
:set all
Shows current settings of all options.
:set option
Enables option.
:set nooption
Disables option.
:set option?
Displays current setting of option.
Common Options
wrapmargin=n
Causes automatic word-wrapping n columns from the
right-hand side of the window in input mode.
autoindent
Autoindent. In input mode, text
will automatically be started
below the first printing character
of the previous line.
Use CTRLD to move back to
previous tab-stops.
number
Line-Numbering.
Line numbers are placed on the left-hand
side of the text, outside the window,
which is shifted to the right.
list
Show hidden characters.
Control characters are represented as
starting with a caret.
(CTRLA = ^ A).
Newline characters are represented
by dollar signs.
showmatch
When a right parenthesis or bracket is
typed in input mode, the cursor will
highlight the matching item if it's
on the screen.
showmode
Place status message in bottom right-hand
corner showing whether vi is in input mode
or not.
report=n
This will cause vi to announce any deletions,
yankings, or puttings of more than n
lines with a message.
Setting report to zero will
cause these activites to be reported
regardless of size.
term=name
Records the terminal type;
this should be automatically set.
The window parameter records
the height (in rows) of the terminal.
Regular
Expressions ^
Beginning of line.
$
End of line.
.
Any single character.
c*
Zero or more occurrences of c.
[abc]
Matches a, b or c.
[^ abc]
Matches anything but a, b or c.
[c1-c2]
Anything in the range c1 to c2.
Escape character for /
End of word
Beginning of word
Substitution
:s/regexp/string/
Replace the first occurrence of regexp
with string on the current line.
See Section 24.5 for a detailed description
of regular expressions and replacement strings.
:s/regexp/string/g
Replace all occurrences of regexp
with string on the current line.
:g/regexp/s//string/
Search the entire file, replacing the
first occurrence on each line of regexp
with string.
:g/regexp/s//string/g
Search the entire file, replacing every
occurrence on each line of regexp
with string.
:g/regexp/s//string/gc
As above, but asking for confirmation
each time: type y
Miscellany
u
Undo last change.
U
Restore all changes on current line.
.
Repeat last change.
~
Reverse case of letter and advance.
J
Join next line to current line.
>move
Shift one tab stop to the right.
>>
Shift current line to the right.
<move
Shift one tab stop to the left.
<<
Shift current line to the left.