Page Banner

Basics of Vi editor in Unix

The Vi editor is a fast and powerful text editor of Unix system. Vi does not contain any menu, instead it uses combinations of keystrokes to accomplish an action. Here I am sharing some useful information about Vi editor.

Modes in Vi editor
Vi has two modes.
1. Command mode- Here you can insert Vi commands. Press Ecs to enter command mode.
2. Insert mode- Here you can insert text. Press i to go to insert mode.

Starting VI editor
Open terminal and type vi followed by a file name. Hit return to enter into the editor. For example,
vi filename.txt

Save and exit VI editor

:wsave file without quitting
:qquit without saving
:wqsave and quit


Moving the cursor

j or <Return> [or down-arrow] move cursor down one line
k [or up-arrow] move cursor up one line
h or <Backspace> [or left-arrow] move cursor left one character
l or <Space> [or right-arrow] move cursor right one character
0 (zero) move cursor to start of current line (the one with the cursor)
$ move cursor to end of current line
w move cursor to beginning of next word
b move cursor back to beginning of preceding word
:0<Return> or 1G move cursor to first line in file
:n<Return> or nG move cursor to line n
:$<Return> or G move cursor to last line in file

 

Adding or inserting text

i insert text before cursor
I insert text at beginning of current line
o open and put text in a new line below current line
O open and put text in a new line above current line
a append text after current cursor position
A append text to end of current line

 

Deleting text

x delete single character in current cursor position
Nx delete N characters, starting with current cursor position
dw delete the single word beginning with current cursor position
dd delete entire line at current cursor position
dNd or Ndd delete N lines, beginning with the current line; e.g., 4dd deletes 4 lines
dNw delete N words starting at current cursor position; e.g., d7w deletes 7 words
D delete the remainder of the line, starting with current cursor position

 

Copying and Pasting

yy copy (yank, cut) the current line
Nyy or yNy copy (yank, cut) the next N lines, including the current line
p paste the copied line/lines into the editor after the current line

 

Searching text

/string search forward for occurrence of string
?string search backward for occurrence of string
n move to next occurrence of search string
N move to next occurrence of search string (opposite direction)

 

Princeton IT is the leading software development company in USA. Get in touch!