Linux Vi Commands

Vi (Visual editor) is a command line text editor that can be found in the UNIX, Linux and macOS operating systems. This is a glossary of some of the commands found in vi.

r Replace one character.
R Replace many characters, until Esc is pressed.
cw change the current word with new text, starting with the character under the cursor, until Esc is pressed.
cNw Change N words beginning with character under the cursor, until Esc is pressed e.g. c5w changes 5 words.
C Change (replace) the characters in the current line, until Esc is pressed.
cc Change (replace) the entire current line, stopping when Esc is pressed.
Ncc or cNc Change (replace) the next N lines, starting with the current line, stopping when is pressed.

yy Copy (yank) the current line into the buffer.
Nyy or yNy Copy (yank) the next N lines, including the current line, into the buffer.
p Put (paste) the line(s) in the buffer into the text after the current line.

x Delete a single character under the cursor.
Nx Delete N characters, starting with the character under the cursor.
dw Delete a single word beginning with character under the cursor.
dNw Delete N words beginning with character under cursor e.g. d5w deletes 5 words.
D Delete the remainder of the line, starting with the current cursor position.
dd Delete the entire current line.
Ndd or dNd Delete N lines, beginning with the current line e.g. 5dd deletes 5 lines.

:.= Returns the line number of the current line at bottom of the screen.
:= Returns the total number of lines at bottom of screen.
^g Provides the current line number, along with the total number of lines, in the file at the bottom of the screen.

:x Quit vi, writing out modified file to file named in original invocation.
:wq Quit vi, writing out modified file to file named in original invocation.
:q Quit (or exit) vi.
:q! Quit vi even though latest changes have not been saved for this vi call.

i Insert before cursor.
I Insert before line.
a Append after cursor.
A Append after line.
o Open a new line after current line.
O Open a new line before current line.

h Move left.
j Move down.
k Move up.
l Move right.
w Move to next word.
W Move to next blank delimited word.
b Move to the beginning of the word.
B Move to the beginning of blank delimited word.
e Move to the end of the word.
E Move to the end of Blank delimited word.
( Move a sentence back.
) Move a sentence forward.
{ Move a paragraph back.
} Move a paragraph forward.
0 Move to the beginning of the line.
$ Move to the end of the line.
1G Move to the first line of the file.
G Move to the last line of the file.
nG Move to nth line of the file.
:n Move to nth line of the file.
fc Move forward to 'c'.
Fc Move back to 'c'.
H Move to top of screen.
M Move to middle of screen.
L Move to bottom of screen.
% Move to associated ( ), { }, [ ].

/string Search forward for occurrence of 'string' in text.
?string Search backward for occurrence of 'string' in text.
n Move to the next occurrence of the search string.
N Move to the next occurrence of the search string in the opposite direction.

Vi can be started from the shell or terminal window.
vi filename Edit the named file starting at line 1.
vi -r filename Recover the named file that was being edited when the system crashed.