Vi Editor ShortCuts
Mastering Text Editing
Basic VI Editor Commands
To start vi:
Example: vi letter will open a new file called letter to edit, or if letter already exists, open the existing file.
Command | Effect |
---|---|
vi filename | Edit filename starting at line 1 |
vi +n filename | Edit filename beginning at line n |
vi +filename | Edit filename beginning at the last line |
vi -r filename | Recover filename after a system crash |
vi +/pattern filename | Edit filename starting at the first line containing pattern |
Command Mode vs. Insert Mode
Insert mode is used for entering text. Command mode is used for issuing commands like moving the cursor, deleting text, copying, pasting, and saving.
To insert text:
Command | Insert Text |
---|---|
i | Before cursor |
a | After cursor |
A | At the end of the line |
o | Open a line below the current line |
O | Open a line above the current line |
r | Replace the current character |
R | Replace characters until Esc, overwrite |
Moving the Cursor
You must be in Command Mode to move the cursor. Each command can be preceded with a Repeat Factor.
Command | Moves the Cursor |
---|---|
SPACE, l, or right arrow | Space to the right |
h or left arrow | Space to the left |
j or down arrow | Down one line |
k or up arrow | Up one line |
w | Word to the right |
b | Word to the left |
$ | End of the line |
0 (zero) | Beginning of the line |
e | End of the word to the right |
– | Beginning of previous line |
) | End of the sentence |
( | Beginning of the sentence |
} | End of paragraph |
{ | Beginning of paragraph |
To Delete Text
The d command removes text. If you delete by mistake, use u (undo) immediately.
Command | Action |
---|---|
d0 | Delete to beginning of line |
dw | Delete to end of word |
d3w | Delete to end of third word |
db | Delete to beginning of word |
dW | Delete to end of blank delimited word |
dB | Delete to beginning of blank delimited word |
dd | Delete current line |
5dd | Delete 5 lines starting with the current line |
dL | Delete through the last line on the screen |
dH | Delete through the first line on the screen |
d) | Delete through the end of the sentence |
d( | Delete through the beginning of the sentence |
x | Delete the current character |
nx | Delete the number of characters specified by n |
nX | Delete n characters before the current character |
Viewing Different Parts of the Work Buffer
Command | Moves the Cursor |
---|---|
^D | Forward one-half screenful |
^U | Backward one-half screenful |
^F | Forward one screenful |
^B | Backward one screenful |
nG | To line n (Ex: 25G moves the cursor to line #25) |
H | To the top of the screen |
M | To the middle of the screen |
L | To the bottom of the screen |
^L | Refresh the screen |
Yanking (Copy) and Putting (Paste) Text
Example: 3yy will yank (copy) 3 lines. p will put the 3 lines just yanked below the current line.
Command | Effect |
---|---|
yM | Yank text specified by M |
y3w | Yank 3 words |
nyy | Yank n lines |
Y | Yank to the end of the line |
P | Put text above current line |
p | Put text below current line |
Changing Text
Example: cw allows you to change a word. The word may be replaced by as many words as needed. Stop the change by hitting Esc.
Command | Effect |
---|---|
cw | Change a word |
c3w | Change 3 words |
Ending an Editing Session
Command | Effect |
---|---|
:w | Writes the contents of the work buffer to the file |
:q | Quit |
:q! | Quit without saving changes |
ZZ | Save and quit |
:wq | Save and quit |
:w filename | Saves to filename (change the name of the file) |
Miscellaneous Commands
Command | Effect |
---|---|
J | Join the current line and the following line |
:set number | Number the lines on the screen |
:set nonumber | Turn off numbering of lines |
:r filename | Reads filename into the current file at the cursor location |
:set showmode | Displays INPUT MODE at the bottom right corner of screen |
~ | Change uppercase to lowercase and vice-versa |
Searching for an Expression
When in command mode, enter /Bill will find the next occurrence of Bill.
Contact Us
For any problems with this webpage, report an accessibility problem or contact howtouselinux.com.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Start Learning Linux today - Free!