A Look at the VI Editor

VI is the widely used text editor in UNIX operating systems. I have had the chance to use the VI editor in the Solaris and Linux operating systems. In this article we are going to learn how to use the VI editor.

VI in Linux and Solaris is almost the same. The main difference I encountered is the arrow keys. In UNIX-based versions you have to use h, j, k and l for left, down, up and right, moving one character/line at a time. But in Linux, we can use the standard arrow keys for editing.

In the Solaris OS, if you use the standard arrow keys during editing you will see some unwanted outputs if the arrow keys are not mapped correctly. Sometimes it may display the error "Terminal too wide" if your terminal is wider than the typical 1024 x 768.

Open a new file/open for editing: To open a new file just use the vi command with a filename like the example below.

vi newfile.txt

This command will open the VI editor with a new file named newfile.txt in the current directory. If a file named newfile.txt is present in the current directory, then the above command will open that file for editing.

{mospagebreak title=Saving and Closing a file}

Use :w for saving the content and :q for quitting. Here are some examples.


Saves current file but doesn’t exit

:w file

Saves current content as file but doesn’t exit

:n,mw file

Saves lines n through m to file

:n,mw >>file

Saves lines n through m to the end of file


Quits VI and may prompt if you need to save


Quits VI without saving

ZZ or :wq

Saves and exits VI

VI has two modes; one is the command mode and the other is the edit mode. To change to the edit mode you have to use i (insert) or a (append). Use the ESC character to change back to command mode.

Inserting texts: Use i, I, a, A, o or O for insertion as described in the following table.


Insert before cursor


Insert before line


Append after cursor


Append after line


Open a new line after current line


Open a new line before current line

{mospagebreak title=Cursor motion/movements}

To move your cursors in VI use the following commands (a long list!).


Move left


Move down


Move up


Move right


Move to next word


Move to next blank delimited word


Move to the beginning of the word


Move to the beginning of blank delimited word


Moves to the first non-blank character in the current line


Moves to the first character in the next line


Moves to the first non-blank character in the previous line


Move to the end of the word


Move to the end of blank delimited word


Move a sentence back


Move a sentence forward


Move a paragraph back


Move a paragraph forward

0 or |

Move to the beginning of the line


Move to the end of the line


Move to nth line of the file

For jumping to the beginning of the document use [[, and use ]] to jump to the end of the document.

Deleting texts: To delete content use d and x. Use x for deleting character to the right of the cursor. Use d with the above operands (motion/movement operands) for deleting. To delete a word use dw (next word). Delete is basically cut, i.e. you can paste the deleted contents. The following are some other delete commands.


Delete character to the right of cursor


Deletes n characters starting with the current one; omitting n deletes current character only


Deletes the character to the left of cursor


Deletes the previous n characters; omitting n deletes the previous character only


Delete to the end of the line


Deletes from the cursor to the end of the line

dd or :d

Deletes the current line


Deletes the next n words starting with the current word


Deletes the previous n words starting with the current word


Deletes n lines beginning with the current line


Deletes lines n through m

Tips: You can edit multiple lines better if you know the line numbers. To show line numbers use the following command.

:set nu

For example, to go to line number 5 use :5

{mospagebreak title=Undo editing}

Use u for undoing the last command. For undoing the last edit on the current line only, unless you move off the line you were editing. use U.

Tips: To perform the last change, use . (period once).

Copying and pasting: You can paste the content you deleted, since the deleted contents are kept in a buffer. Use p to paste. Use yy to copy the current line or use :y for copying (or yanking) the current line, and use yw to copy the buffer contents ("yw" for "yank work"). You can use yyp together to copy and paste the current line. Use p to put the buffer contents after the position or after the line. Use P to put the buffer contents before the position or before the line.

You can copy the content in different buffers from a to z, and you can also paste from these buffers by specifying the buffer before the paste command like (a-z)p or (a-z)P. Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. For example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.

Searching: Use / for searching in the forward direction and, use ? for searching in the backward direction. The following are some examples.


search down for "str" in forward direction


search up for "str" in backward direction

You can ignore cases during searching by using the set command.

Google+ Comments

Google+ Comments