The University of Kansas
Department of Electrical Engineering and Computer Science


vi Editor

vi editor is one of the fundamental, but yet powerful editor which is available in most unix systems. Although many other editors may have better performances over vi, it is not a bad idea to learn how to use vi, especially for newcomers of using UNIX systems.

There are a summary of vi command, by topic:

vi filename

a enters text after the cursor.

h moves the cursor to the left one character.

j moves the cursor down the line.

k moves th cursor up the line.

l moves the cursor to the right one character.

x deletes the current character.

CR carriage return; moves the cursor down to the beginning of the next line.

ESC escape; leaves text input mode and returns to command mode.

ZZ writes to a file and quite vi.

:q quits vi if changes made to the buffer were written to a file.

Commands for positioning in the window

b movres the cursor to the left one word, to the first character of the that word.

B moves the cursor to theh left one word, (delimited only by blanks) to the first character of that word.

e moves the cursor to the end of the current word.

E moves the cursor to the end of the current word (delimited only by blanks); places the cursor on the last character before the next blank space or at the end of the current line.

Positioning by sentences

( moves the cursor to the beginning of the current sentence.

) moves the cursor to the beginning of the next sentence.

Positioning by paragraphs

( moves the cursor to the beginning of the current sentence.

) moves the cursor to the beginning of the next sentence.

Positioning in the window

H moves the cursor to the first line on the screen,or "home".

M moves the cursor to the middle line on the screen.

L moves the cursor to the last line on the screen.

BS backspace; moves the cursor one character to the left.

space bar moves the cursor one character to the right.

fx moves the cursor right to the specified character x.

Fx moves the cursor left to the specified character x.

; continues the search for the character specified by the f command. It will remember the command and seek out the next occurence of the character on the currnet line.

tx moves the cursor left to the character just after the specified character x.

Tx moves the cursor right to the character just after the specified character x.

Positioning by lines

+ moves the cursor down one line.

- moves the cursor up one line.

Word Positioning

w moves the cursor to the right, to the first character in the next word.

W Ignores all punctation and moves the cursor to the right, to th ebeginning of the next word delimited only by blanks.

Commands for Positioning in the file

ctrl f scrolls the screen forward a full window, revealing the window of the text below the current window.

ctrl d scrolls the screen down half a window, revealing the window of the text below the current window.

ctrl b scrolls the screen back a full window, revealing the window of the text above the current window.

ctrl u scrolls the screen up a half window, revealing the window of the text above the current window.

Positioning on a numbered line

G moves the cursor to the beginning of the last line in the buffer.

ctrl g gives the line number, position in the buffer (as a percentage of the buffer completed), and status.

nG moves the cursor to the ith line of the file(n = line number).

Searching for a Pattern

/pattern searches forward in the buffer for the next occurence of the pattern of text. Positions the cursor under the first character of the pattern.

?pattern searches backward in the buffer for the next occurence of the p attern of text. Positions the cursor under the first character of the pattern.

n repeats the last search command.

N repeats the search command in the opposite direction.

A enters text at the end of the current line.

i enters text to the right of the cursor.

I enters text to the right of the cursor that is not a blank on the current line.

o enters text at the beginning of a new line immediately below the current line.

O enters text at the beginning of a new line immediately above the curre nt line.

Delete commands

for the TEXT INPUT Mode

BS Backspace; deletes the current character.

ctrl w deletes the current word delimited by blanks.

@ erases the current line of text.

for the COMMAND Mode

u undoes the last command.

U erases the last change on the current line.

x deletes the current character.

dw deletes a word (or part of a word) from the cursor through the next space or to the next punctuation.

dd delete the current line.

ndx deletes n number of text objects x. x can be the symbol for a word, line, current sentence, or current paragraph.

D deletes the current line from the cursor to the end of the line.

Line Editor commands

: tells vi that the next commands are line editor commands.

:sh temporaily returns to the shell to performs some shell commands without leaving vi.

:n goes tothe nth line of the buffer.

:x,zw data writes line s from the number x through th enumber z into a new file called data.

:$ moves the cursor to the beginning of the last line in the buffer.

:.,$d deletes all the lines from the current line to the last line.

:r filename inserts the contents of the file filename under the current line of hte buffer.

:s/text/new words/ replaces the first instance of text on the current line with new words.


A note by Sachverstand2:

On MY version of vi, this doesn't work. Instead, you have to say
:%s/text/new words/
that is add a percent sign between the : and the s.
Maybe I could have added 1,$ instead. Both mean the same: do from start of file to end of file.
:s/text/new words/g replaces every occurence of text on the current line with new words.

:s/text/s//new words/g replaces every occurence of text in the buffer to with new words.

Special command

. repeats the action initialised by the last command.

J joins the line immediately below the current line with the current line.

\ prints characters that are normally nonprinting(hidden characters) in the test input mode.

Change Command

r replaces the current character.

R replaces only those characters typed over with new characters;contents to spend new text after the end of the line unitl the ESC command is given.

s deletes the current characters in the current line.

S replaces all the characters in the current line.

cw replaces all the current word or the remaining characters in hte current word with new text, from the cursor to the next space or punctuations.

cc replaces all the characters in the current line.

ncx replaces n number of text objects x,. x can be the symbol for a word, line , current sentence, or current paragraph.

C replaces the remaining characters in the current line, from hte cursor ofthe end of the line.

Cut and Paste command

P places the contents of theh temporary buffer contaiining the last 'delete' or 'yank' command into the text after the cursor or below the current line.

.yy places (yanks) a line of text into a temporary buffer.

nyx places a copy of n number of text intoa temporary buffer.

"lyx places a copy of text object x into a register named by a letter l. x can be the symbol for a word, line, current sentence, or current paragraph.

"xp places the contents of the register x after the cursor or below the current line.

ctrl v prints characters that are normally nonprinting(hidden characters) in the test input mode.

ctrl l clears and redraws the current window.

~ change uppercase to lowercase or vice versa.

Quit Commands

ZZ writes the buffer to a file and quit vi.

:wq writes the buffer to a file and quite vi.

:w filename, :q writes the buffer to a new file named filename and quite vi.

:w! filename, :q overwrites an existing file named filename and quite vi.

:q! quit vi whether or not changes made to the buffer were written in the file.

:q quit vi if changes made to the buffer were written in th e file.

Special options for vi

vi file1 file2 file3 enters three files into the vi buffer to be editede. Those files are file1, file2, and file3.

:w, :n when editing more than one file, writes the buffer to a file called filename and calles the next file in the buffer(use only after :w).

vi -r file1 restores the changes made to files1 that were lost because of an interrupt in the system.

view file1 views file1 in hteread-only mode of vi. Changes cannot be made to the buffer.

For more information, type "man vi" in your unix system environment platform(s).

This page is written and quoted by Tim Yan, and accedited to the contributor(s) of vi editor.