Basic VI Editor Commands

The vi editor is available on almost all Unix systems. vi can be used from any type of terminal because it does not depend on arrow keys and function keys--it uses the standard alphabetic keys for commands. This help note explains the basics of vi: opening and closing a file, moving around in a file and elementary editing.

Starting vi

vi filename Edit a file named "filename".

vi newfile create a new file named "newfile".

Entering text

i Insert text left of cursor.

a Append text right of cursor.

Moving the cursor

h Left one space.

j Down one line.

k Up one line.

l Right one space.

Basic editing

x Delete character.

nx Delete n characters.

X Delete character before cursor.

dw Delete word.

ndw Delete n words.

dd Delete line.

ndd Delete n lines.

D Delete characters from cursor to end of line.

r Replace character under cursor.

cw Replace a word.

ncw Replace n words.

C Change text from cursor to end of line.

o Insert blank line below cursor (ready for insertion).

O Insert blank line above cursor (ready for insertion).

J Join succeeding line to current cursor line.

nJ Join n succeeding lines to current cursor line.

u Undo last change.

U Restore current line.

Moving around in a file

w Forward word by word.

b Backward word by word.

$ To end of line.

0 (zero) To beginning of line.

H To top line of screen.

M To middle line of screen.

L To last line of screen.

G To last line of file.

1G To first line of file.

<Control>f Scroll forward one screen.

<Control>b Scroll backward one screen.

<Control>d Scroll down one-half screen.

<Control>u Scroll up one-half screen.

n Repeat last search in same direction.

N Repeat last search in opposite direction.

Closing and saving a file

ZZ Save file and then quit.

:w Save file.

:q! Discard changes and quit file.

Copyright © 1991 by yhang, Simon Fraser University.

Last update: 2019-05-24T17:05:14+00:00