The vi editor (visual editor) is a screen editor which is available on the almost all Unix systems. Once you have learned vi, you will find that it is a fast and it is the powerful editor. vi has no menus but instead of that, it uses combinations of keystrokes in order to accomplish commands.

The name “vi” is derived from the shortest unambiguous abbreviation for the ex command visual, which switches the ex line editor to visual mode.

vi filename:

It creates a new file if it already has not existed, otherwise, it opens an existing file and it starts at line 1.

Create the new file or open the existing file in vi:

Vi”  without any filename will open a new file, where we can enter the text and edit but while coming out it will ask to enter the valid filename to save the text. Vi with a filename as an argument will open that file for editing if the file already exists, it open it otherwise it creates a new file by the argument.

Example: $vi abfile

It creates or opens the existing file called abfile.

Modes in vi:

vi operate in following of two modes, they are:

i. Command Mode:

When a file is opened in a command mode, that is, input from the keyboard will be treated as vi commands and you will not see the words which you are typing on the screen.

ii. Insert Mode:

To enter the text you have to put vi in insert by pressing ‘i’ or ‘a’, after that which you can add the text and whatever has been typing will be seen on the screen. To switch between these mode Esc key is used.  Esc i  is used for (text mode) and  Esc is for (command mode).

Saving  & Exiting  vi editor:

You can exit vi in different ways, they are

  1. Quit without saving: If we don’t  want to save the work “:q”  will take you out without saving our editing in vi.

      2. Write & quit : . Simple the  “:w” saves the current file but don’t exit. For save and quit  “:we” is used in vi.

      3.  Forced Quit: An ! (Exclamation sign at the end of exit commands (:q! , :wq! )  causes a forced to quit from vi after ignoring the editing (for:q!)  or writing (for:wq!)  all the changes.

Moving within a File:

To move around within a file without affecting our text, we must be in the command mode (press Esc twice). The following table lists out a few commands that we can be used to move around one character at a time.

 k  –  It Moves the cursor up one line.

 j   – It Moves the cursor down one line.

h   It Moves the cursor to the left one character position.

l   –  It Moves the cursor to the right one character position.

The following points that need to be considered to move within a file,

1. vi is case-sensitive. That we need to pay the attention to capitalization when we using the commands.

2. Most commands in vi can be prefaced by the number of times we want the action to occur.

          For example,  The 2j moves the cursor two lines down the cursor location.

Control Commands:

The following commands can be used with the Control Key to perform the following functions

The list of control commands is given below.

Editing Files:

To edit the file, we need to be in the insert mode. There are many ways to enter the insert mode from the command mode,

i –  It Inserts text before the current cursor location

I – It Inserts text at the beginning of the current line.

a – It  Inserts text after the current cursor location

A –  It Inserts text at the end of the current line

o –  It Creates a new line for the text entry below the cursor location

O – It Creates a new line for the text entry above the cursor location

Deleting Characters:

The list of important commands, which can be used to delete characters and lines in an open file,

 x –  It Deletes the character under the cursor location

X – It Deletes the character before the cursor location

dw –  It Deletes from the current cursor location to the next word

 d^ –  It Deletes from the current cursor position to the beginning of the line

d$ – It Deletes from the current cursor position to the end of the line

D –  It Deletes from the cursor position to the end of the current line

 dd –  It Deletes the line the cursor is on

As mentioned above, the most commands in vi can be prefaced by the number of times that we want the action to occur. For example, 2dd deletes two lines the cursor is on and 2x deletes two characters under the cursor location.

Change Commands:

The characters, words, or lines in vi can be changed without deleting them. The relevant commands are,

cc   It Removes the contents of the line, leaving you in insert mode.

cw  It Changes the word the cursor is on from the cursor to the lowercase w end of the word.

r     It Replaces the character under the cursor. vi returns to the command mode after the replacement is entered.

R    It Overwrites the multiple characters beginning with the character currently under the cursor. We must use Esc to stop the overwriting.

s     It Replaces the current character with the character you type. Afterward, we are left in the insert mode.

S     It Deletes the line and the cursor are on and replaces it with the new text. Then after the new text is entered, vi remains in the insert mode.

Copy and Paste Commands:

The lines or words can be copied from one place to another place. The commands for copy and paste are,

 yy   It copies the current line.

yw  It copies the current word from the character and the lowercase w cursor is on, until the end of the word.

 p    It puts the copied text after the cursor.

P     It puts the yanked text before the cursor.

Advanced Commands:

The day-to-day editing can be simplified by using some advanced commands and also allows the more efficient use of vi. The list of advanced commands are given below,

Word and Character Searching:

The vi editor has two kinds of searching: string and character. For the string search, the / and? commands are used. When you start these commands, the command just is shown on the last line of the screen, where we type the particular string to look of,

These two commands differ only in the direction where the searching takes place,

  1. The / command search forwards (downwards) in the file.
  2. The ? command search backward (upwards) in the file.

The n and N commands repeat the previous search command in the same or the opposite direction, respectively. Some characters have special meanings. These characters must be preceded by a backslash (\) to be included as a  part of the search expression.

^   It Searches at the beginning of the line.

.    It Matches a single character.

*    It Matches zero or more of the previous character.

$   It the end of the line (Use at the end of the search expression).

[    It Starts a set of matching or non-matching expressions.

 <   This is put in an expression escaped with the backslash to find the ending or the beginning of a word.

>    This helps to see the above ‘<‘ character description.

The character search searches within one line to find a character to be entered after the command. The f and F command searches for characters on the current line only. f searches forwards and F searches backward and the cursor moves to the position to find a character.

The t and T command search for a character on the current line only, but for the t, the cursor moves to the position before the character, and T searches the line backward to the position after the character.

Categorized in:

Tagged in: