vi editor commands in Linux or Unix OperatingSystems

vi Commands

These commands are used in UNIX / LINUX.

Using vi command, we can create and modify files

When we open Vi by default it will be in command mode if we press ( I, i, a, A), It will
be turn into insert mode.

We type (vi file1) command to open a file or create a file

INSERTING COMMANDS :

1. (i) : It means a character/data allows before the cursor position
2. (I) : To insert Before the first character of the current line

Append commands :

(a) – To append after current cursor position
(A) – To append at the end of the current line

(o) – To open a newline after the current line
(O) – To open newline before the current line

Delete commands :

(x) – To delete a single character at cursor position
(X) – To delete a single character before the cursor position

(dd) – To delete entire line/current line
(D) – To delete from cursor position to end of the line
(dw) – To delete a word

 Replace commands :

(r) – To replace a single character at cursor position
(R) – To replace entire multiple characters until we press escape key

 Cursor movement commands :

0 (zero) – To move the cursor to the first character of the line (including space)
($) – To move the cursor to the end of the line

(j) – To go to the next line
(k) – To move the cursor to the previous / above the cursor line
(h) – To move the cursor to leftside

(l) – To move the cursor to the right

Note 1 : We need to click the escape key before entering commands if it is in insert mode.

 Use escape key to turn from insert mode to command mode.

SAVE AND EXIT COMMANDS :

( : ) (w) (q)
:wq + enter (it is used to save and quit the vi)
:w – it is used to save the file
:q – it is used to quit
:q! – It is used to quit without saving

REPLACE COMMAND :

r — to replace single character

R — to replace more than one character until we press escape key

This command is used to convert lowercase to uppercase and viseversa
Shift + tild (~)

Colon commands : Last line mode commands

If any command starts with ( : ) then it is known as Last line mode commands

: set nu  to get line numbers
: 20  to place the cursor at 20 th line
: $  to move the cursor to the last line
: 1  to move the first line
: 1co$  to copy the specific line needed in the last (in this number 1 we can’t change)
: 1co10  to copy and paste the first line after the 10 th line
: 1m15  to move first line to the 15 th line it is replaced to specific line
: 1, 4co$  to copy multiple line to end of the file (in this number can be changed)

 

see also