Learn How To Use Linux vi Editor And Its Commands

"vi" is the standard text editor in Unix and Linux systems. It is a visual editor as opposed to line editors "ed" and "ex". vi can be a little uncomfortable to use at first if you are new to it or have been using other advanced editors like 'gedit'. But once you get used to it, it can be really simple and fast to work around with vi. In this article, we discuss the working modes of vi and how to get used to it. But in the end, practicing will make you comfortable and efficient in vi.

Starting vi.

A file can be edited using vi by typing vi at command line and passing the file name as an argument to it.

vi filename

This will open the file in vi editor. The changes you make in the file are stored in the buffer until you save them. If the file provided as the argument does not exist, then a new file with that name will be created if the changes made are not abandoned.

Working modes of vi.

The vi editor works in three modes:

1. Command mode.
2. Insert mode.
3. ex mode or last line mode.

The command mode is the default mode of vi. When you open a file in vi, you are in command mode. You can change to insert mode and ex mode from command mode only. You cannot directly enter ex mode from insert mode and vice versa. The keys you type in command mode will be interpreted as commands and not as part of the text. Insert mode is the mode in which the keys entered are the part of the document. In ex mode, tasks like saving the file and exiting from the vi editor (of course among others) are performed.

Moving around the file.

While in command mode, you can move your cursor from one position to another. The basic cursor movement commands are:

h - left.
j - down.
k - up.
l - right.

It means that pressing 'h' key while in command mode will move your cursor left of current position. These 4 keys move the cursor one letter at a time. But moving around a large document using these keys will take a lot of time. We can move by word, sentence or even by paragraph.

By word:

w - forward one word.
b - backward one word.

By Sentence:

) - forward one sentence.
( - backward one sentence.

By Paragraph:

} - forward one paragraph.
{ - backward one paragraph.

There are some additional movement commands:

$ - move to end of current line.
0 - move to the beginning of current line.
gg - move to beginning of the document.
G - to the end of the document.

Insert mode.

You can enter the insert mode from the command mode by a number of commands. Once you are in this mode, all the keys you press will appear in the document. The first command is 'i'. Pressing 'i' in command mode will take you to insert mode and you will be able to insert before the cursor position.

'a' command will change the mode to command mode and place the cursor ahead of current position, allowing you to "append" after the current cursor position.

'o' will "open" a new line below the current line and the cursor is placed in that line. This will also take you to insert mode.

'I' will insert at the beginning of the current line.
'A' will append to the end of current line.
'O' will open up a new line above the current line.

To move back from insert mode to command mode, use esc key. Pressing esc key more than once will not do anything. In command mode, esc key has no action.Further, you cannot enter ex mode directly from this mode.

Basic editing commands.

You can perform tasks like copy, cut and paste from the command mode.

Yank (copy)

y command is used to copy (or yank) the text. But y command does not copy by itself. You need to specify what you want to copy.

yy - yank line.
yw - yank word.
yl - yank letter.

Similarly, we can also copy in larger amounts using (, ), { and } commands. These commands will have their usual meaning as in the movement commands.

y( - yank sentence behind the cursor position.
y) - yank the sentence after the cursor position.
y{ - yank paragraph above.
y} - yank paragraph below.

Delete (cut)

To cut or delete the text, d command is used. Similar to y (yank) command, d command is also used with w,l,(,) etc.

dd - delete line.
dw - delete word.
dl - delete letter.
d( - delete sentence behind.
d) - delete sentence ahead.
d{ - delete paragraph above.
d} - delete paragraph below.

Paste.

The yanked (copied) or deleted (cut) text can be put (or pasted) using 'p' or 'P' command. It will depend upon the nature of text how the text will be put. For letters and words, pressing 'p' will put the text after the cursor position and pressing 'P' will put the text before it. For a line or a paragraph, 'p' will paste below the current line and 'P' will place it above the current line.

Undo.

If you made a change in the document by mistake, you can undo it using 'u' or 'U' while in the command mode. To undo the most recent change, press 'u' while in the command mode. 'U' will undo all the changes to the current file.

Repeating commands n number of times.

The commands like copy, delete and most of the other commands like cursor movement commands can be repeated n number of times by pressing that number before the command. For example, pressing 3yy in command mode will yank (copy) 3 lines. 12dw will delete 12 words from the current cursor position. Similarly, to move ahead 10 lines, 10) can be used. 6h will move the cursor 6 positions left to current cursor position.

Search in the document.

You can search the current document for a pattern like you can in "less". When in command mode, press / followed by the pattern to be searched. For example, if you want to search the word "kernel" in the document, then you will type "/kernel" (without quotes) and then press enter. This will search the document for the phrase 'kernel' below the current cursor position. If you want to search upwards in the document, use '?' instead of '/'. Pressing "?kernel" followed by return key will search the document upwards for the phrase 'kernel'. Pressing 'n' will continue the search for the next match in the same direction (e.g. downward for /) and 'N' will search in the opposite direction.

Save (or don't) and exit.

When you have made all the changes you want to the document, then you might want to save your changes and/or exit from the editor. These functions are performed in the ex mode. To enter the ex mode from command mode, : (colon) is used. To save all the changes to disk from the buffer, :w is used. :q will quit from the current document if the buffer does not have any unsaved changes. To save changes and quit from document, the above two commands are combined, i.e. :wq will save your changes and quit from the vi editor.

To exit without saving changes, use :q! command. (Note that you need to press enter after these ex mode commands unlike the commands in command mode).The changes can be saved in another file as well (the save as option). To save the changes in a file named myfile.txt (say), you will type:

:w myfile.txt
or
:wq myfile.txt

Additional ex mode commands.

:set can be used to configure the behavior of vi editor. For example, to display line number along with the lines, use the following ex mode command:

:set number
or
:se nu (for short)

To switch off the line numbers, use “:set nonumber” or “:se nonu”.

You can execute a command from vi, without leaving it using ":![command]". For example:

:!ls
:!date

This will execute ‘ls’ and ‘date’ commands respectively and show the result on the screen. Pressing enter will return the control to the vi editor.

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.