Systems Engineering and RDBMS

Back to the basics: Frequently used VI editor commands

Posted by decipherinfosys on April 4, 2008

VI is most popular editor on UNIX. Today we will recap some of the most frequently used VI editor commands. To edit the file using VI, use vi filename. Following is the list of frequently used VI commands.

Single character commands:

a => append after the cursor
A => append at the end of the line
b => move back one word at a time.
B => move back last word before the space.
C => change entire line
e => move cursor at the end of the next word.
G => takes to the end of the file.
h => move one position on left
H => takes to the first line or top of the display screen. If it is preceded with number, then it takes you to that line. e.g. 5H takes to the 5th line from the top.
i => insert from current position or before cursor.
I => insert at the beginning of the line.
j => move one line down.
J => append the next line to the current line on which cursor is positioned.
k => move one line up.
l => move one position on right.
L => takes to last line or bottom of display screen.
m => set the mark. e.g. ma will mark current position as a. similarly we can use mb or mc etc. to mark different positions. Marked positions can be recalled from anywhere in the file using ‘a or ‘b or ‘c. ( ‘ is single quote key left of the <ENTER> key.)
n => repeat last search in forward direction. If you reach the end of the file, then search will again start from the beginning of the file.
N => repeat last search in backward direction. If you reach the beginning of the file, search will again start from the end of the file.
o => open the new line after current line.
O => open the new line before current line.
p => insert from buffer. (yanking ‘yy’ will put lines in buffer)
r => replace current character where cursor is positioned. It can be replaced by other single character.
R => replace characters from current position onward, one character at a time until ‘Esc’ key is pressed.
s => substitute current character. Substitution could be one or more characters.
S => substitute entire line.
u => undo previous change.
U => undo entire line.
w => move one word forward.
x => delete current character.
{ => move back one paragraph. Paragraph is considered as text between two blank lines.
} => move forward one paragraph
% => match the parenthesis
~ => reverse the case (if upper then do it lower and vice versa).
$ => move to last column.
. => repeat the last command.
0 => move to beginning of the line.
Esc => exit out of the edit mode.

Double character commands:

dd => delete the current line. If number precedes dd, then those many lines will be deleted. e.g. 10dd will delete 10 lines from current position.
cc => change entire line
yy => yank/copy the line in buffer. It can be placed in the file using p option. If number precedes yy, then those many lines will be copied in buffer. e.g. 10yy will copy 10 lines in buffer, which can be put into file anywhere using p.
cw => change word.
ce => change word.
ZZ => save changes and exit the file.

Commands with <Ctrl> Key:

Ctrl-l => refresh screen
Ctrl-b => move one screen backward.
Ctrl-f => move one screen forward.
Ctrl-g => status of the file.

These are some of the basic and frequently used commands used in VI editor. For a full reference sheet for the VI commands, you can just google for “vi commands” and you will find a lot of good concise information.

One Response to “Back to the basics: Frequently used VI editor commands”

  1. […] Back to the basics: Frequently used VI editor commands […]

Sorry, the comment form is closed at this time.

 
%d bloggers like this: