Skip to main content

Basic vi editing command

You must be in command mode to execute editing commands. Many of the editing commands are case sensitive. They have different meaning depending on whether they are typed as lowercase or upper case. Also, editing commands can be preceded by a number to indicate a repetition of the command.
Deleting characters:
1. X : will delete the character before the cursor
2. 4x : This will remove four characters
Deleting Words: Make sure to position the cursor to the first letter of word
1. dw : it will delete the word and space follwing it.
2. 3dw : will delete three words

Deleting Lines: Make sure the cursor is at the beginning of the line
1. dd : delete the entire line.
2. 2dd : delete two lines
3. D : To delete from the cursor postion to the end of the line.

Replacing Characters: This will replace one character with another
1. Position the cursor to the character to be replaced.
2. Then type r
3. Type the new character you want to replace with.
4. Now, the new character should appear and you should still be in the command mode.

Replacing word: move the cursor to the start of the word you want to replace
1. cw : vi will change itself to Inster mode and the last letter of the word to be replaced will turn into a $.
2. 3cw : will replace three words
3. Press “ESC” to get back to command mode.

Replacing Lines: This will change text from the cursor position to the eend of the line.
1. Type C
2. Type the replacement text
3. Press “ESC”

Inserting Text:
1. Position the cursor where you want the new text to appear.
2. Type “i”
3. Now you can start entering text
4. Press “ESC” to go back

Navigating a file:
w : forward word by word
b : backward word by word
$ : to end of line
0 : to the beginning of line
H : to top line of screen
M : to middle line of screen
L : to last line of screen
G : to last line of file
1G : to first line of file
CTRL + f : scroll forward one screen
CTRL + b : Scroll backward one screen
CTRL + d : Scroll down one half screen
CTRL + u : Scroll up one half screen

Search Text:
While in command mode,
1. Type /
2. Enter the text you want search
3. Press “Enter” Key
4. The cursor moves to the first occurrence of the text you typed above after slash
5. Type n to search in forward direction and N to search in backward direction

Saving and Closing the file:
Save the file and quit vi: Shift + ZZ
Save only and leave the file open:
1. Press ESC
2. Type :w
3. Press “Enter”
Don’t save and quit vi:
1. Press ESC
2. Type :q!
3. Press “Enter”