OpenWindows Advanced User's Guide

6.12 Summary of Basic vi Commands

The following table provides a convenient reference for basic vi commands.

Table 6-1 Basic vi Commands

Command 

Meaning 

Starting vi

 

vi filename

Open or create file 

vi

Open new file to be named later 

vi -r filename

Recover crashed file 

view filename

Open file read-only 

Cursor Commands

 

h

Move left one character 

j

Move down one line 

k

Move up one line 

l

Move right one character 

w

Move right one word 

W

Move right one word (past punctuation) 

b

Move left one word 

B

Move left one word (past punctuation) 

e

Move to end of current word 

Return

Move down one line 

Backspace

Move left one character 

Spacebar

Move right one character 

H

Move to top of screen 

M

Move to middle of screen 

L

Move to bottom of screen 

Ctrl-F

Page forward one screen 

Ctrl-D

Scroll forward one-half screen 

Ctrl-B

Page backward one screen 

Ctrl-U

Scroll backward one-half screen 

Inserting Characters and Lines

 

a

Insert characters to right of cursor 

A

Insert characters at end of line 

i

Insert characters to left of cursor 

I

Insert characters at beginning of line 

o

Insert line below cursor 

O

Insert line above cursor 

Changing Text

 

cw

Change word (or part of word) to right of cursor 

cc

Change line 

C

Change from cursor to end of line 

s

Substitute string for character(s) from cursor forward 

r

Replace character at cursor with one other character 

r Return

Break line 

J

Join current line and line below 

xp

Transpose character at cursor and character to right 

~

Change case of letter (upper or lower)

u

Undo previous command 

U

Undo all changes to current line 

:u

Undo previous last-line command 

Deleting Text

 

x

Delete character at the cursor 

X

Delete character to the left of the cursor 

dw

Delete word (or part of word to right of cursor) 

dd

Delete line containing the cursor 

D

Delete part of line to right of cursor 

dG

Delete to end of file 

d1G

Delete from beginning of file to cursor 

:5,10 d

Delete lines 5-10 

Copying and Moving Text

 

yy

Yank or copy line 

Y

Yank or copy line 

p

Put yanked or deleted line below current line 

P

Put yanked or deleted line above current line 

:1,2 co 3

Copy lines 1-2 and put after line 3 

:4,5 m 6

Move lines 4-5 and put after line 6 

Setting Line Numbers

 

:set nu

Show line numbers 

:set nonu

Hide line numbers 

 

Setting Case-sensitivity

:set ic

Searches should ignore case 

:set noic

Searches should be case-sensitive 

Finding a Line

 

G

Go to last line of file 

1G

Go to first line of file 

21G

Go to line 21 

Searching and Replacing

 

/string

Search for string

?string

Search backward for string

n

Find next occurrence of string in search direction

N

Find previous occurrence of string in search direction

:g/search/s//replace/g

Search and replace 

Clearing the Screen

 

Ctrl-L

Clear (refresh) scrambled screen 

Inserting a File into a File

 

:r filename

Insert (read) file after cursor 

:34 r filename

Insert file after line 34 

Saving and Quitting

 

:w

Save changes (write buffer) 

:w filename

Write buffer to named file 

:wq

Save changes and quit vi 

ZZ

Save changes and quit vi 

:q!

Quit without saving changes