Saturday, October 16

vi Editor In Linux

vi editor stands for the visual editor. It is the default editor that comes with Unix/Linux distribution. You can create, edit a file using vi editor. If you do not wish to modify the file, then just read it with the help of the vi editor.

[root@linuxhawks ~]# vi filename

Modes of operation in vi editor

There are basically three modes of operation in vi editor i.e. Command mode, Insert mode and Escape mode.

1. Command mode

Initially, when a file opens in vi editor, it is in Command mode. In this mode, whatever character you type vi interprets as a command and hence does not display on the screen. This mode allows us to move through a file, and to delete, copy, or paste a piece of text.
To enter into Command mode from any other mode, you need to press the [Esc] key. If you are already in Command mode and press the [Esc] key, you will hear a beep sound or maybe a flash on the screen.

Commands and their description
: Inserts text after current cursor location
: Inserts text at end of current line
i : Inserts text before current cursor location
: Inserts text at beginning of current line
h : Moves the cursor to the left one character position
: Moves the cursor to the right one character position
: Moves the cursor down one line
k : Moves the cursor up one line
: Creates a new line for text entry below cursor location
: Creates a new line for text entry above cursor location
: Replace single character under the cursor with the next character typed
: Replaces text from the cursor to right
: Replaces single character under the cursor with any number of characters
S : Replaces entire line
: Positions cursor at end of line
: Positions cursor to the next word
: Positions cursor to previous word
: Positions cursor to beginning of current sentence
) : Positions cursor to beginning of next sentence
: Move to top of screen
nH : Moves to nth line from the top of the screen
: Move to middle of screen
: Move to bottom of screen
nL : Moves to nth line from the bottom of the screen
X Uppercase: Deletes the character before the cursor location
x Lowercase : Deletes the character at the cursor location
Dw : Deletes from the current cursor location to the next word
d^ : Deletes from current cursor position to the beginning of the line
d$ : Deletes from current cursor position to the end of the line
Dd : Deletes the line the cursor is on
Yy : Copies the current line
9yy : Yank current line and 9 lines below
p : Puts the copied text after the cursor
P : Puts the yanked text before the cursor
CTRL+d : Move forward 1/2 screen
CTRL+f : Move forward one full screen
CTRL+u : Move backward 1/2 screen
CTRL+b : Move backward one full screen
CTRL+e : Moves screen up one line
CTRL+y : Moves screen down one line
CTRL+u : Moves screen up 1/2 page
CTRL+d : Moves screen down 1/2 page
CTRL+b : Moves screen up one page
CTRL+f : Moves screen down one page
CTRL+I : Redraws screen

2. Insert Mode

You can enter anything in the file using this mode. To come in insert mode you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.

3. Escape mode or Last line mode

Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will jump to the last line of the screen and vi will wait for a command. This mode enables you to perform tasks such as saving files, executing commands.

Commands and their description
q : Quit
q! : Quit without saving changes i.e. discard changes
r filename : Read data from file called filename
wq : Write and quit (save and exit)
w filename : Write to file called filename (save as)
w! filename : Overwrite to file called filename (save as forcefully)
!cmd : Runs shell commands and returns to Command mode

vi also has powerful search and replace capabilities. The formal syntax for searching is:


The syntax for replacing one string with another string in the current line is:


Here “pattern” represents the old string and “replace” represents the new string.
The syntax for replacing every occurrence of a string in the entire text is similar. The only difference is the addition of a “%” in front of the “s”:



Leave a Reply

Your email address will not be published. Required fields are marked *


Enjoy this blog? Please spread the word :)