The vi Text Editor - Common Commands

The vi text editor is a full-screen text editor found on UNIX systems. There are two modes in vi: insert mode and command mode. In insert mode, text is entered in the document. In command mode, you can move within the document, merge lines, search, cut, and so on. You can carry out ALL of the functions of vi from command mode except enter new text. Text can only be entered when in insert mode.

vi file

Evoke vi editor (enters command mode) on file. If it doesn't exist, it will be created.

i, a, I, A

Insert text before, after cursor, beginning of line, end of line [Puts you into INSERT MODE].

o, O

Open new line for text below, above cursor [Puts you into INSERT MODE].

r, R

replace one, or more characters [Only R puts you into INSERT MODE].

ESC

Move from insert mode to command mode.

h, j, k, l

Move left, down, up, right one space (or use the arrow keys).

w, W, b, B

Move forward, backward one word.

0, $

Move to first, last position of current line.

CTRL-F, CTRL-B

Scroll forward, backward one screen.

CTRL-D, CTRL-U

Scroll down, up one-half screen.

CTRL-L, CTRL-R

Redraw screen.

:n (colon n)

Move to line number n.

CTRL-G

Display current line number. n

/pattern, ?pattern

Search forward, backward for pattern.

n, N

Repeat last search in same, opposite direction.

x, dw, dd, D

Delete (cut) current character, word, line, rest of line.

yy

Yank (copy) current line.

p, P

Put deleted (or yanked) text before, after cursor.

J

Join two lines.

u, . (period)

Undo last command. Repeat last command.

:wq (or ZZ), :w

Save file (write) and quit vi. Save file and stay in vi.

:q! (colon q bang)

Quit, without saving changes. The UNIX shell escape is ! (exclamation or bang).

:!javac %

Compile current file (stay in vi). Outside vi use javac file.java.

:!java file

Execute file (stay in vi).

%

Find matching brace {, }, [, ], (, )

Most vi commands can be preceded by a count. For example, 5w moves forward five words and 5dw deletes the next five words. There are many, many more commands and functions available in vi. The following website has an excellent vi tutorial: https://engineering.purdue.edu/ECN/Support/KB/Docs/ViTextEditorTutorial/index_html