The Vim Editor

Vi is visual editor used to enter and edit text files containing data or documents or programs. It displays the contents of files on the screen and allows the user to add, insert, delete or change parts of the text.

Modes of operation

The Vi program has three modes of operation:

Command/Esc Mode

In this mode all the keys pressed by the user are interpreted to be editor command mode. In command mode the keys that are hit are not displayed on the screen.

Insert Mode

This mode permits the insertion of new text, editing of existing text or replacement of existing text. Each of these operations can be performed only after changing over from the command mode to insertion mode using appropriate commands. The insertion mode is also known as input-text mode.

The Colon Mode

This mode permits us to give commands at the command line. The bottom line of the VI editor is called the command line. All commands entered in the colon mode are displayed in the command line.

Getting started with vi

The vi editor is invoked by the following command at the UNIX $ prompt

vi filename <RET>

If the give file does not exist then vi displays a blank screen with a ( ~) tilte character at the beginning of each line as follows:

If the file exists, then its contents are read in to the memory and also displayed on the screen. Assume that the given file is a new file and the user wishes to enter text into his file. In vi to add, change, delete text etc., commands have to be given. These commands are summarized in the following pages.

CommandAction
iInsert text at current text position
aAppend text after cursor position

The above command allows a user to start entering text. The user has to press i. Key or the ‘a ‘ key and then start entering the text. To end insertion or appending of text, the user has to press <ESC> key. The user thus keeps changing between command mode and edit mode.

Assume that the entered some text in the file and presses the <ESC> key after entering the second line. This takes him out of edit mode and command mode meaning that he cannot make the changes to lines but can only give another command. The <ESC> key toggles between the command mode and the edit mode.

Cursor Movement Commands of vi

The basic cursor movement commands are:

CommandAction
HMoves the cursor one position to the left.
LMoves the cursor one character to the right. K Moves up one line in the same column.
JMoves down one line in the same column.
-Moves to the beginning of previous line.
^DScroll down half screen.
^UScroll up half screen.
^FPage forward.
^BPage backward.
NgGo to nth line.
0(zero)Beginning of the line.

Insert and Replace Commands

CommandAction
AAppend after current character.
AAppend at the end of the line.
IInsert before current character.
OInserts a blank line below and allows insertion.
OInserts a blank line above and allows insertion.
r-Replaces current character.
RReplaces characters with text.

Word Movement Commands

CommandAction
WMoves to the first character of the next word
BMoves to the first character of the previous word.
EMoves to the end of the current word

Deletion and Modification commands

CommandAction
DwDelete current word.
DdDelete a line.
CwChange word.
CcChange line.
x-Delete character at cursor position.
XDelete character before cursor position.
JJoin lines.
u-Undo last change.
URestore last change.
. (dot)Repeat last change.

Commands to Copy Lines

CommandAction
YyYank or anchor on current line (which is to be copied)
NyyYank n number of lines.
PPaste yanked line after cursor position.
PPaste yanked text before current position.

Saving and Exiting Commands

CommandAction
:wSave all changes made so far.
:wqSave all changes and quit.
:qQuit vi
:q!Quit without saving changes.

Customizing vi

  • VI can be tailored to behave in a way desirable to the user.

  • There are a number of commands available in the ex mode which make it suitable for writing programs as well as preparing documents.

  • You make like to set a pleasant and comfortable environment for yourself so that the editor behaves and responds to your directives exactly in the way you would like it to.

  • There is also the facility to store all the user-set options in a separate file, which is used by the editor to take its startup instructions from.

  • This is the file .exrc containing all user-defined settings – conceptually similar to the .profile which is maintained by each user.

The set command

There are a large number of set commands available, though you hardly need to know to use more than a couple of them. To display all the set options, you have to issue the following instruction:

:set all

This displays all the options, and also indicates whether they are operative or not. When the string no is prefixed to any of these options, it indicates that the operation is inoperative. Some of them also use abbreviations, though at this stage you needn’t take the trouble to know.

Subscribe For More Content