You are on page 1of 6

General Startup

To use vi: vi filename


To exit vi and save changes: ZZ or :wq
To exit vi without saving changes: :q!
To enter vi command mode: [esc]

Counts
A number preceding any vi command tells vi to repeat
that command that many times.

Cursor Movement

h move left (backspace)

j move down

k move up

l move right (spacebar)

[return] move to the beginning of the next line

$ last column on the current line

0 move cursor to the first column on the


current line

^ move cursor to first nonblank column on the


current line

w move to the beginning of the next word or


punctuation mark

W move past the next space

b move to the beginning of the previous word


or punctuation mark

B move to the beginning of the previous word,


ignores punctuation

e end of next word or punctuation mark

E end of next word, ignoring punctuation

H move cursor to the top of the screen

M move cursor to the middle of the screen

L move cursor to the bottom of the screen


Screen Movement

G move to the last line in the file

xG move to line x

z+ move current line to top of screen

z move current line to the middle of screen

z- move current line to the bottom of screen

^F move forward one screen

^B move backward one line

^D move forward one half screen

^U move backward one half screen

^R redraw screen
( does not work with VT100 type terminals )

^L redraw screen
( does not work with Televideo terminals )

Inserting

r replace character under cursor with next


character typed

R keep replacing character until [esc] is hit

i insert before cursor

a append after cursor

A append at end of line

O open line above cursor and enter append mode

Deleting

x delete character under cursor

dd delete line under cursor

dw delete word under cursor

db delete word before cursor

Copying Code
yy (yank)'copies' line which may then be put by
the p(put) command. Precede with a count for
multiple lines.

Put Command
brings back previous deletion or yank of lines,
words, or characters

P bring back before cursor

p bring back after cursor

Find Commands

? finds a word going backwards

/ finds a word going forwards

f finds a character on the line under the


cursor going forward

F finds a character on the line under the


cursor going backwards

t find a character on the current line going


forward and stop one character before it

T find a character on the current line going


backward and stop one character before it

; repeat last f, F, t, T

Miscellaneous Commands

. repeat last command

u undoes last command issued

U undoes all commands on one line

xp deletes first character and inserts after


second (swap)

J join current line with the next line

^G display current line number

% if at one parenthesis, will jump to its mate

mx mark current line with character x

'x find line marked with character x

NOTE: Marks are internal and not written to the file.


Line Editor Mode
Any commands form the line editor ex can be issued
upon entering line mode.

To enter: type ':'

To exit: press[return] or [esc]

ex Commands
For a complete list consult the
UNIX Programmer's Manual

READING FILES
copies (reads) filename after cursor in file
currently editing

:r filename

WRITE FILE

:w saves the current file without quitting

MOVING

:# move to line #

:$ move to last line of file

SHELL ESCAPE
executes 'cmd' as a shell command.

:!'cmd'

Some more topics

Text Buffers in VI
The VI editor has 36 buffers for storing pieces of text, and also a general
purpose buffer. Any time a block of text is deleted or yanked from the file, it
gets placed into the general purpose buffer. Most users of VI rarely use the
other buffers, and can get along without the other buffers. The block of text is
also stored in another buffer as well, if it is specified. The buffer is specified
using the " command. After typing ", a letter or digit specifying the buffer must
be entered. For example, the command: "mdd uses the buffer m, and the last
two characters stand for delete current line. Similarly, text can be pasted in
with the p or P command. "mp pastes the contents of buffer m after the
current cursor position. For any of the commands used in the next two
sections, these buffers can be specified for temporary storage of words or
paragraphs.

Indenting Your Code and Checking


The VI editor has features to help programmers format their code neatly.
There is a variable that to set up the indentation for each level of nesting in
code. For example, the command to set the shift width to 4 characters is :set
sw=4.
The following commands indent your lines or remove the indentation, and can
be specified with count:
<<
Shifts the current line to the left by one shift width.
>>
Shifts the current line to the right by one shift width.

The VI editor also has a helpful feature which checks your source code for any
hanging parentheses or braces. The % command will look for the left
parenthesis or brace corresponding to a particular right parenthesis or brace
and vice versa. Place the cursor onto a parenthesis or brace and type % to
move the cursor to the corresponding parenthesis or brace. This is useful to
check for unclosed parentheses or braces. If a parenthesis or brace exists
without a matching parenthesis or brace, VI will beep at you to indicate that no
matching symbol was found.

Word and Character Searching


The VI editor has two kinds of searches: string and character. For a string
search, the / and ? commands are used. When you start these commands, the
command just typed will be shown on the bottom line, where you type the
particular string to look for. These two commands differ only in the direction
where the search takes place. Some characters have special meanings to VI,
so they must be preceded by a backslash (\) to be included as part of the
search expression.
Special characters:
^
Beginning of the line. (At the beginning of a search expression.)
.
Matches a single character.
*
Matches zero or more of the previous character.
$
End of the line (At the end of the search expression.)
[
Starts a set of matching, or non-matching expressions... For example: /f[iae]t
matches either of these: fit fat fet In this form, it matches anything except
these: /a[^bcd] will not match any of these, but anything with an a and
another letter: ab ac ad
<
Put in an expression escaped with the backslash to find the ending or
beginning of a word. For example: /\<the\> should find only word the, but not
words like these: there and other.
>
See the '<' character description above.

Creating .exrc file to customize vi

Below is a sample of .exrc file present in HOME directory. It can be used to


customize the vi editor options.

set showmode
map #2 :set number
map #3 :set nonumber
map #5 :Please
map del dd
map quit :q!
map save :wq!
map src :/
map copy yy

You might also like