Table of Contents

This is part 10 of the series Linux Command Line Interface. Please check earlier articles to get the grasp of current one.

Introduction

VIM is improved version of text editor called VI. The name VI is derived from the command visual, which was used in line editor called ex in older days.

VI is a powerful text editor standardized by POSIX and Single Unix Specification (SUS). So It comes with every Linux distribution released.

VI or VIM editor operates in two modes.

  • Command Mode
  • Insert Mode
VI text editor
VI text editor

Starting VI Editor

Command vi followed by file name is used to start VI editor.

When we open VI text editor, the default mode will be command mode. We can’t write anything into file in this mode.

[email protected] ~ $ vi prog.c 

If the file prog.c doesn’t exist, it will be created. If file exists, the content of the file will be shown. The editor looks like following:

~
~
~
~
~
~
~
~
~
~
~
~
~
~
"prog.c" [New File]                                           0,0-1         All

Inserting Text

Simply pressing the key i changes vi editor from command mode to insert mode. You should see the text – INSERT – at the bottom of terminal. Now we can enter text into the file.

There are other ways to get into insert mode. Press Esc to return to command mode and then follow these commands. Note that vi editor should be in command mode to enter any command.

CommandOperation
iInserts text to the left of the cursor.
IInserts text at the beginning of the line, no matter where the cursor is positioned on the current line.
aBegins inserting after the character (append) on which the cursor is positioned.
ABegins inserting at the end of the current line, no matter where the cursor is positioned on that line.
oBegins inserting text on a new, empty line, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.
OBegins inserting text on a new, empty line, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.
rReplace single character under cursor position.
RReplace all characters, starting under cursor position, until Esc is pressed.

In insert mode we can write text just like any other editors. Arrow keys can be used for cursor movement. Backspace and Delete keys can be used for removing text.

Saving and Exiting

There are certain commands we use to save the content and exit this vi editor. We use these commands in command mode.

CommandOperation
:wSaves Current File
:qQuits Editor
:q!Forces the Editor to Quit. Changes may lost
:wqChanges Saved and Quit
:wq!Forces Editor to Save Content and Quit

Copying, Pasting and Deleting

Though we can manage editing files with just above commands and operations, following commands are used for quick and efficient editing.

CommandOperation
DDelete Text from Cursor Position to End of the Line.
ddDelete Entire line at Cursor
nddDelete n number of lines. Ex: 2dd, 5dd etc.
xDeletes/Cuts Character at Cursor Position
XDeletes/Cuts Character Before Cursor (Just like Backspace).
yCopy Text at Cursor
YCopy Entire Line at Cursor
pPaste Text at Cursor Position
PPlace Text Before Cursor Position

A Simple Example Using VI Editor

Let’s create a simple C Program file called prog.c. Open the Terminal.

Enter the command vi prog.c and it looks like following.

~
~
~
~
~
~
~
~
~
~
~
~
~
~
"prog.c" [New File]                                           0,0-1         All

Press i. Now VI editor enters into insert mode. You should see – INSERT – at the end.

Now type the program (or any content). Here I am writing a simple Hello World! program. This looks like following.

#include <stdio.h>
int main(void)
{
        printf("Hello World!\n");
        return 0;
}
~
~
~
~
~
~
-- INSERT --                                                  5,11-18       All

Next step is to save the file. Before we enter any command, we need to change VI editor into command mode. Press Esc key. – INSERT – must be gone. Now type :w and press Enter. This should save your file. To quit vi editor enter the command :q.