“Vi” Editor


UNIX Editor


An editor is a utility program that is used to make modification to the contents of a file or files. No doubt, editing is one of the most frequently used capabilities of a general purpose system. An editor is designed to deal with text files. Many users prefer to use an editor to create or modify their files. Therefore, an editor interactive utility program by which the user can make any changes that can be viewed by that time.

UNIX provides basically three types of editors in almost all its versions: “ed”, “ex”, and “vi”. The “ed” also known as “line editor” and was the first editor. The “ed” editor assigns line number to each line in the file. The “ex” editor is an improved version of an ed editor. The “vi” editor stands for visual editor, which is more popular than the earlier tow editors “ed” and “ex”.

The “vi” editor is developed by the University of California at Barclay by Bill Joy when he was a student there.

The “vi” editor is a Screen editor rather than a line editor that allows the user to see some part of his file on the terminal’s screen. The “vi” editor shows the user as much of the file contents as it can fit on the screen. The vi editor is the first full screen editor.


“vi” Editing Modes


The “vi editor works in three different modes:

(i) Command mode:

When the vi editor is started, it is placed in command mode. In the command mode, whenever the user presses any key, it is interpreted to be editor command. Note that the keys, that are hit, are not displayed on the screen in command mode.

(ii) Text Input Mode:

This mode is used when a new text is added; existing text is edited or replaced. We can not come to text input mode directly. We change over from the command mode to text mode with any of the following subcommands:  The a, A, s, S, i, I, R, o, O, c, C commands. After using one of these commands, we can enter the text.

After completing the text work, we can return to command prompt by pressing either the “Esc” key or the “Ctrl – c”. This mode is also known as “Insert mode”.

(iii) The ex Command Mode:

The subcommand with the prefix  : (colon), ? (question mark), / (Slash), or !! read input on a line displayed at the bottom line of the screen. The bottom line of the vi screen is called the command line.

All commands entered in the ex command mode are displayed in the command line. The vi editor uses the command line to display messages and commands. Enter key is pressed to execute the subcommand or interrupt (Ctrl – c) key to cancel it.


Introduction to "vi" editor


The vi editor is invoked by executing, just like any other UNIX command, along with the filename as:

$ vi filename

After typing this and pressing Enter key, the contents of the specified file are stored into the edit buffer. The terminal screen is cleared, a window is displayed in which you can enter and edit text, and the cursor is initially set at the first character of first line in the window.

Suppose you would like to create a file “punit” then invoke the vi editor as:

$ vi punit

It clears you previous contents of the screen and displays a window as shown in figure.




“rohit “


Except the first line and the last line, the window shows a character ~ (tilde), the symbol of empty line. The first line has an underscore (_) in the first colon that waits for a command to enter by you.

By default the vi editor is placed in command mode and that’s why it waits for you to enter a command. If you want to insert text to the file “punit” then press “i” key to enter the insert mode of vi. When you press the “I” key, it is not displayed on the screen. Now you can enter the text whatever you want to insert in figure.


Rohit Sharma

Mukesh kumar

Ajay kumar



“rohit” 3 lines and  31 characters


After typing these names, press the Esc key to return the command mode from the current mode.


Editing a file


If you want to edit a file then it is necessary to load the file from a disk into buffer and then move to the part of the file you want to edit. And it is achieved as:

$ vi punit

The vi program will clear your screen and fills it with file you are editing as shown in figure.

The cursor is placed at first character of the first line. You can move the cursor by using the different commands.

rohit Sharma

Mukesh kumar

Ajay kumar



“rohit” 3 lines and  31 characters


Moving within a file


Following screen command are used to move the cursor.


h          moves the cursor one character to the left

I           moves the cursor one character to the right

K         moves the cursor one line up while it remains in the same column character to the left

J           moves the cursor one line down while it remains in the same column character to the left


Diagrammatically it may be represented as:




These commands are displayed on the screen. The current window screen contains only the contents of the file. After placing the cursor to the right position you can edit the file. Unfortunately if you see these commands on the screen then it means that you are still in insert mode. Therefore, firstly press Esc key to return back to command prompt and then try these motion commands again. You can also proceed h, j, k, l command with numbers that allows you to move the cursor a corresponding number of spaces or lines. Let you want to move 4 lines  down then either press “j” four times or just type 4j and press the Enter Key.


Some other commands that help in positioning the cursor :

Backspace                   one character left

Spacebar                      one character right

$                                  moves the cursor to the end of the current line

+                                  moves the cursor up to the beginning of next line


Adding Text


The first thing you have to do is that place the cursor at position where you want to add and then press ‘a’. The ‘a’ stands for append. Now whatever you will type is appended to the text buffer after the current cursor position.

Now when you press Esc key, the cursor returns back to the last character you entered. It tells you that you are no longer in insert mode.

The vi editor provides another command ‘i’ command that works similar to ‘a’ command. The only difference between the ‘a’ and ‘i’ command is that it inserts the new characters before the current cursor position. Therefore after pressing ‘i’ whatever you type, it will be added to the left of the characters on which your cursor is placed


Deleting Text


The vi editor provides two commands, ‘x’ and ‘dd’, that delete text. If you want to delete a character then firstly place the cursor at the appropriate position. and then press ‘x’ key. After pressing ‘x’, the character at the current cursor position will disappear and the rest of the line would be moved one position left side.

If you want to delete the 4 characters of a line then just type 4x, this delete the four characters of the current line and move the line to the left side.

Whereas ‘dd’ command is used to delete the complete line. You can also used numbering with “dd” command. e.g if you want to delete 3 lines, then just press ‘3dd’, it will delete the 3 lines of the files.


Undo command


UNIX also provide undo command. For this we use character ‘u’. Suppose after deleting three lines, you think that you had to delete one line only instead of three lines then use an ‘u’ command to get back the most recently deleted lines.


Overwriting Text


The vi editor provides a ‘R’ command to overwrite the existing contents of a file. Therefore, whenever you want to overwrite, place the cursor at that special position and then press R. Now whatever you type next would be overwritten the existing text at the current cursor position.


Saving a file


The vi editor also provides the ‘ZZ’ (capital in nature) command to save all the changes that you make to the file and quit. This command is used in command mode.


Moving from word to word


The vi editor provide the facility of moving the cursor from word to word such as using ‘w’ command for next word and ‘b’ command for previous word. Some commands are given below:



w           moves the cursor to the first character of next word.

b             moves the cursor to the first character of previous word.

e              moves the cursor to end of the current word.


Scrolling command


Scrolling is the property of a video terminal that makes the top line to leave the screen as each new line is displayed at the bottom. The vi editor provide following command for scrolling purpose


Ctrl – U          Scroll up one – half screen

Ctrl – D          Scroll down one – half screen

Ctrl – F           Scrolls forward one screen

Ctrl – B           Scrolls backward one screen


Block Commands


The commands that we have discussed so far deals with a single character, single word or a single line. The vi editor provides block commands to work on a group of lines rather that on a single line. For example, we may copy a group of lines from one part of a file to another part, delete a group of lines from a file by using a single command. The block command are used to work in ex command mode only. Although it is not necessary to associate  line numbers with the text because block commands work on line number on which they are supposed to operate.

For example you have a file that contain 20 lines and you want to delete lines from 6 to 10, then you use the command;

6, 10d

Such command delete the 6 to 10 lines.

The vi editor provides a command ‘set number’ or ‘set nu’ in the ex command mode that enables you to display line number. Thus if you are in command mode and want to display line number then press the colon (:) key that takes you in ex command mode. Now the cursor is placed at the bottom line of the screen and you type in the command:

(Esc): set number


(Esc): set nu

The vi editor provide some other useful block commands as:

  1. :m,n w file: This command writes lines m to n to a file.
  2. :m,n w>> file: This command appends lines m to n to a file.
  3. :r files: This reads contents of the specified file at current cursor position.
  4. :r! command: It executes shell command and output of the command is read at the current cursor position.



Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet