Search

Howto create and edit text files using vi editor, basic vi commands, command mode, insert mode

The vi editor (visual editor) is the default text editor in many UNIX and UNIX like Operating Systems including Linux. vi editor is an advanced text editor with many features.

vi editor and has two modes of operation:

• Command mode: In command mode, commands can be issued to the vi editor which cause action to be taken on the file.

• Insert mode: In insert mode the file can be edited.

File opening, closing and exit commands

• To open a file - vi filename (If the file is not existing, new file can be created)

• To recover the file from a crash run vi -r filename

•To go to insert mode, use the command "i" or "a" (the characters typed in will be inserted after the current cursor position).

• To go back from insert mode to command mode in vi editor type "Esc".

• To Exit vi editor (from the command mode of the vi editor run the following commands)

 

 

Command

 

Description

:x <Return>     

quit vi editor, with writing out modified file to disk.

:wq<Return>    

quit vi editor, with writing out modified file to disk.

:q<Return>

quit (or exit) vi editor

:q!<Return>

quit vi editor even though latest changes have not been saved

:w file

vi editor will write file as file, leaving original untouched

 

Moving the cursor in vi editor

 

 

Command

 

Description

j or down-arrow

move cursor down one line in vi editor

k or up-arrow  

move cursor up one line in vi editor

h or left-arrow

move cursor left one character in vi editor

l or right-arrow

move cursor right one character in vi editor

0 (zero)

move cursor to start of current line in vi editor

$

move cursor to end of current line in vi editor

W

move cursor to beginning of next word in vi editor

B

move cursor back to beginning of preceding word in vi editor

:0<Return> or 1G

move cursor to first line in file in vi editor

:n<Return> or nG

move cursor to line n in vi editor

:$<Return> or G

move cursor to last line in file in vi editor

 

Screen Movement Commands in vi editor


 

Command

 

Description

z+

move current line to top of screen in vi editor

z

move current line to the middle of screen in vi editor

z-

move current line to the bottom of screen in vi editor

^f

move forward one screen in vi editor

^b

move backward one screen in vi editor

^d

move down (forward) one half screen in vi editor

^u

move up (back) one half screen in vi editor

^l

redraws the screen in vi editor

 

Cut, Copy and Paste operation commands in vi editor

 

 

Command

 

Description

x

delete single character; nx deletes n characters (n – any number) in vi editor

X

deletes the character to the left of your cursor in vi editor.

Dw

delete word; ndw deletes n words (n – any number) in vi editor

Dd

delete line; ndd deletes n lines (n – any number) in vi editor

Cw

delete word and start in insert mode (i.e. change word) in vi editor

Cc

delete line and start insert mode (change line) in vi editor

S

delete character and start insert mode (change character) in vi editor

D

delete the remainder of the line, starting with current cursor position in vi editor

Yy

copy (yank, cut) the current line into the buffer; nyy copy (yank, cut) the next n lines (n – any number) in vi editor

Yw

Copy a word; nyw copies n words (n – any number) in vi editor

 

Other Useful commands in vi editor

 

 

Command

 

Description

Guu

Convert complete line to Lower-case in vi editor

gUU

Convert complete line to Upper-case in vi editor

U

Undo last change in vi editor

^r

redo in vi editor

/text

Search for text (forwards) in vi editor

/

Repeat forward search in vi editor    

?text

Search for text (backwards) in vi editor

?

Repeat previous search backwards in vi editor

:.=

returns line number of current line at bottom of screen in vi editor

:=

returns the total number of lines at bottom of screen in vi editor

Related Tutorials