Vi and Vim Basics
When you run vi
or vim
it is started in command mode. In order to switch to edit/insert mode press i
. Now you can edit the current line. To go back to the command mode press Esc
. Insert modes include: i
, I
, a
, A
, o
, and O
.
To quit type :q
. To save the changes and quit type :wq
. To discard the changes and quit type :q!
. To save the file at the current stage type :w
.
Save the file and close the vi
editor using any one of the following methods that saves changes:
Command | Function / Keys |
---|---|
:x
|
Will save and close the file. |
:wq
|
Will write to file and quit. |
:wq!
|
Will write to a read-only file, if possible, and quit. |
ZZ
|
Will save and close. Notice that no colon : is used in this case.
|
:q!
|
Exit without saving changes |
:e!
|
Discard changes and reload file |
:w!
|
Write to read-only, if possible. |
To get help type :help
or :help <topic-name>
, or :helpgrep <phrase>
. To go back to the editor type :q
– note you nay need press :
more than one time to reach the command line at the bottom (reference).
Within the command mode you can navigate via the file's content and do some changes by the following keys / commands.
Key / Command | Function | |
---|---|---|
j
|
↓
|
Moves cursor down one line (same as down arrow). |
8j
|
2j
|
Move 8 or 2 lines down. |
k
|
↑
|
Moves cursor up line (same as up arrow). |
8k
|
2k
|
Move 8 or 2 lines up. |
l
|
→
|
Moves cursor to the right one character (same as right arrow). |
8l
|
2l
|
Move 8 or 2 characters to the right. |
h
|
←
|
Moves cursor to the left one character (same as left arrow). |
8h
|
2h
|
Move 8 or 2 characters to the left. |
w
|
Shift+W
|
Moves cursor to beginning of next word. Move one word forward. |
2w
|
5w
|
Move 2 or 5, or n words forward.
|
e
|
Moves cursor to end of word | |
b
|
Moves cursor to beginning of previous word | |
$
|
End
|
Moves cursor to end of current line (same as End key) |
0 (zero)
|
Home
|
Moves cursor beginning of current line (same as Home key) |
3G
|
Jumps to third line (nG jumps to the nth line)
| |
1G
|
Jumps to first line | |
Shift+G
|
Jumps to the last line | |
dw
|
Delete Word. | |
2dw
|
3dw
|
Delete 2 or 3, or n words.
|
u
|
Undo. | |
2u
|
5u
|
Undo last 2 or 5, or n commands.
|
x
|
Delete a character. | |
xx
|
xxxx
|
Delete 2 or 4 characters. |
2x
|
7x
|
Delete 2 or 7, or n characters.
|
X
|
Delete a character to the left of the cursor. | |
XX
|
XXXX
|
Delete 2 or 4 characters to the left of the cursor. |
2X
|
5X
|
Delete 2 or 5, or n characters to the left of the cursor.
|
dd
|
Delete a line. | |
2dd
|
3dd
|
Delete 2 or 3 or n lines.
|
p
|
Paste the deleted line(s) below the current line. | |
Shift+D
|
d$
|
Delete the characters from the current cursor position to the end of the line. |
Shift+J
|
Join two lines, the current and the next. | |
3J
|
5J
|
Join 3 or 5, or n lines
|
yw
|
Copy (or “yank”) the current word. More precisely from the current cursor position to the end of the word. | |
Shift+P
|
Paste (or “put”) the copied word before the current cursor position. | |
Shift+`
|
~
|
Change the next letter to upper case if it is lowercase and vice versa. |
o
|
Open a blank line below (to the cursor position) and start edit/insert mode. Lowercase "o". | |
O
|
Open a blank line above (to the cursor position) and start edit/insert mode. Uppercase "O". | |
cw
|
Override. When you are at the beginning of a word when press cw you will be in insert mode and you will be able to type over a word.
| |
i
|
Add text at the current cursor position. | |
I
|
Add text at the beginning of a line. | |
a
|
Start edit/insert mode to append text to the right of the cursor. | |
A
|
Add text at the end of a line. |
Search for and replace the word text
with TEXT
:
:%s/text/TEXT/g
Search for the next occurrence of the word line
:
/line
- To search for the next instance of the same word (pattern) press
n
.
Search backward for the word line
:
?line
- To search for the next instance of the same word (pattern) press
n
.