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:
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.
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.
|i||Insert text at current text position|
|a||Append 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:
|H||Moves the cursor one position to the left.|
|L||Moves the cursor one character to the right. K Moves up one line in the same column.|
|J||Moves down one line in the same column.|
|-||Moves to the beginning of previous line.|
|^D||Scroll down half screen.|
|^U||Scroll up half screen.|
|Ng||Go to nth line.|
|0(zero)||Beginning of the line.|
Insert and Replace Commands
|A||Append after current character.|
|A||Append at the end of the line.|
|I||Insert before current character.|
|O||Inserts a blank line below and allows insertion.|
|O||Inserts a blank line above and allows insertion.|
|r-||Replaces current character.|
|R||Replaces characters with text.|
Word Movement Commands
|W||Moves to the first character of the next word|
|B||Moves to the first character of the previous word.|
|E||Moves to the end of the current word|
Deletion and Modification commands
|Dw||Delete current word.|
|Dd||Delete a line.|
|x-||Delete character at cursor position.|
|X||Delete character before cursor position.|
|u-||Undo last change.|
|U||Restore last change.|
|. (dot)||Repeat last change.|
Commands to Copy Lines
|Yy||Yank or anchor on current line (which is to be copied)|
|Nyy||Yank n number of lines.|
|P||Paste yanked line after cursor position.|
|P||Paste yanked text before current position.|
Saving and Exiting Commands
|:w||Save all changes made so far.|
|:wq||Save all changes and quit.|
|:q!||Quit without saving changes.|
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
.exrccontaining all user-defined settings – conceptually similar to the
.profilewhich 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:
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.