Basic vi Reference Sheet


 

Starting vi

 

vi

Starts vi without a file

vi filename

Starts vi and loads filename

 

 

Getting Help

 

:viusage

   (or)

:help

Lists all vi commands

:viusage command

   (or)

:help command

Gives short help on the vi command command

 

 

Moving the Cursor

 

l

Right one space

h

Left one space

k

Up one line

j

Down one line

w

Right one word

b

Left one word

$

End of line

0 (zero) or ^

Beginning of line

)

End of sentence

(

Beginning of sentence

}

End of paragraph

{

Beginning of paragraph

 

 

Moving the View

 

Ctrl-D

Forward one-half screenful

Ctrl-U

Backward one-half screenful

Ctrl-F

Forward one screenful

Ctrl-B

Backward one screenful

 

 

Searching/Replacing

 

/regex

Search forward for regex

?regex

Search backward for regex

n

Repeat original search exactly

N

Repeat original search in the opposite direction

:s[address]/search-string/replacement-string/[g]

Replace search-string with replacement-string.

·         [address] is one number or two numbers separated by a comma, indicating which lines to search in.  A . (dot) indicates the current line, and a $ (dollar sign) indicates the last line.

·         If [g] is present, it will replace more than once per line, otherwise it won’t.

 

 

 

Adding Text

(These commands put vi in “insert mode”. To return to “command mode”, press ESC.)

i

Insert text before cursor

I

Insert text before first nonblank character on line

a

Insert text after cursor

A

Insert text at end of line

o

Open a line below current line

O

Open a line after current line

r

Replace the current character (no need to use ESC)

R

Replace characters, starting with the current character (ESC quits)

 

 


Deleting Text

 

x

Delete the current character

X

Delete the character before the current character

dd

Delete the current line

D

Delete to the end of the current line

 

 

Miscellaneous

 

J

Joins the current line and the following line

.

Repeats the most recent command that made a change

:w filename

Write the file as filename

:q

Quit vi

:q!

Quit vi without saving changes

ZZ

Writes to current filename and exits

 

 

Copying and Pasting

(the”Delete” commands also copy to the buffer)

yy

Yank the current line to the unnamed buffer

Y

Yank to the end of the current line to the unnamed buffer

P

Puts buffer text before the cursor position

p

Puts buffer text after the cursor position