RHCE Study Guide

This tutorial explains vi and vim editor in easy language with examples including difference between vi and vim, advantage of vim over vi and vi editor modes.

Basic concepts of Vi Editor

Vi is the text based editor. It was developed by Bill Joy in 1976. It includes all basic features for creating, editing and updating a text file. Even though there are several other advanced features rich editors available nowadays, vi is still popular and most widely used editor. If you are a person who calls someone else to repair the system, you may ignore this editor. But if you are person who is called to repair the system, you must learn this editor. From an end user’s point of view it is a bulky and non-user friendly editor but from an administrator’s point of view it is a life saver editor. The reason why administrators like this editor at first place is that this is the only editor which is available in all situations. No matter where you are in Linux, vi editor will be there for you.

Difference between vi and vim editor

Vim (vi-improved) is the enhanced and improved version of vi editor. It was developed by Bram Moolenarr in 1991. It includes everything which vi has. Besides all benefits of vi, vim provides following advantages over vi editor: -

  • It includes more features for programing language such as syntax highlighting, code folding, text formatting etc.
  • It includes inbuilt utility for comparing files.
  • It includes undo/redo facility.
  • Vim supports external scripting language.
  • It can edit compressed files.
  • It can edit remote files over network protocol.
  • It supports plugins for additional functionality.
  • It supports screen splitting for editing in multiple files simultaneously.
Tips

Functionally, vim and vi are same. If you are learning from scratch, I will suggest you to use vim editor instead of vi editor. Vim is the cloned version of vi, when we learn how to use vim editor, we will automatically learn how to use vi editor.

Vi (Vim) Modes

There are two important modes; command mode and insert mode. Command mode is the default mode. When we open a file with vi editor, we are placed in this mode. This mode is used to control the editing operations such as cut, copy, paste, delete, move, replace, select, remove, navigation, save and exit. We are not allowed to edit or insert text in this mode. Insert mode is used to edit or insert the text in file.

How keyword will function is depend on which mode is selected. If command mode is selected then keys will be used to execute the commands. If insert mode is selected then keys will be used to edit the text in file. To switch between both modes, escape key is used.

vi mode

Before we understand both modes practically, let’s create a test directory.

We should always use a separate directory for practices. When we are finished with all exercises, we can delete this directory. This way we can complete this tutorial without making any mess in existing file system.

#mkdir test

Move in this directory.

#cd test

Now run following command

#vim regular-file-vim

Following figure illustrates above commands.

vim test file

Vim will search for the supplied file name. If file exist, it will open that file for editing. If file does not exist, it will create new file with supplied name and open it for editing.

vi new file

As we know by default vi will open file in command mode and we are not allowed to insert any text in command mode. To switch in insert mode escape key + i key combination is used. Let’s switch in insert mode and edit file with some dummy contents.

vi command insert mode

In insert mode we can only edit or insert the text. We cannot perform any control operation in this mode. To switch back in command mode escape key is used. Let’s switch back in command mode. In command mode use : + w +q keys combination to save and exit from file.

vi command save and exit

Let’s repeat this exercise once again, but this time we will use vi command instead of vim command.

Open same file again with vi command

vi existing file

Switch in insert mode and edit the file

vi insert mode

Now save and exit form file

vi save and exit command

So far we are working with regular files; there is no noticeable difference between vi and vim. To see the difference between vi and vim edit any script or programming file. Let’s copy fstab file in test directory.

cp fstab

To see the difference, open it with both editors one by one.

vi fstab file

As we can see in above output, vim formats file in more use friendly manner. So far functionality is concerned both editors work in same manner. Which editor we choose is the matter of personal choice.

Vi (Vim) Editor commands

We have learned how to create a new file and open an existing file. In this section we will learn how to edit an opened file step by step.

How to insert or append text in vi (vim) editor

Cursor position is used as the base line while working with texts. Where exactly we want to work in file is determined from cursor position for example whether we want to insert the text before the cursor or after the cursor.

Some common insert or append operations are following.

  • To insert the text before the cursor, use Escape key + i key.
  • To append the text after the cursor, use Escape key + a key.
  • To insert the text in the beginning of line, use Escape key + I key.
  • To insert the text in the end of line, use Escape key + A key.

Open the file which you created for practice and perform all above operations.

vi command inseart operation

How to insert new blank line

  • To insert a new blank line above the line, use Escape key + O key.
  • To insert a new blank line below the line, use Escape key + o key.

By default only one line will be inserted. To insert multiple lines we have to specify the number of lines before O/o key. For example to insert 3 blank lines above the cursor line we have to press Escape (key) + 3 (key)+ O (key) combination.

Following figure illustrates line insert operation.

insert line in file vi command

How to cut and paste in vi (vim) editor

To cut a line dd command is used. To paste the contents from memory buffer p command is used. By default both operations will be performed single time. It means, dd command will cut a single line and put it in memory buffer and p command will paste the contents from memory buffer one time. To cut multiple lines or to paste the contents from memory buffer multiple times we have to specify the number before the command. For example to cut three lines, press Escape + 3 + d +d keys combination. Just like it, to paste the contents four times, press Escape + 4 + p keys combination.

Following figure illustrates cut and paste operation.

vi command cut operation

How to copy and paste in Vi (vim) editor

Copy and paste operation is similar to cut and paste operation. To copy a line yy command is used. To paste the contents form memory buffer same p command is used. To perform the operation in multiple lines, use number of lines before yy command. For example to copy 4 lines, press Escape + 4 + y + y keys combination.

  • To copy a single character from cursor, use yl command
  • To copy a single word from cursor, use yw command.

Following figure illustrates copy and paste operation.

how to copy in vi command

How to find text in Vi (vim) editor

To find a text string in forward direction Escape + / + [String] command is used. To find a text string in backward direction Escape + ? + [String] command is used. To navigate between search results n key is used with Escape key. To move in backward direction Escape + N key is used while to move in forward direction Escape + n key is used.

Following figure illustrates search operation

how to search in vi command

How to find and replace text in Vi (Vim) Editor

To replace first occurrence of text use Escape + : + % + s + / + [old text] + / [new text] keys combination where old text is the text which you want to replace and new text is text from which you want to replace the old text. To replace all occurrences of text use Escape + : + % + s + / + [old text] + / [new text] + / +g keys combination.

Following figure illustrates find and replace operation.

vi command find and replace opeeration

How to delete text in Vi (Vim) editor

To delete a single character from cursor in forward direction use Escape + x keys combination. To delete a single character from cursor in backward direction use Escape + X keys combination.

vi command delete characters

To remove a single word after the cursor use Escape + d + w keys combination. To remove a single word before the cursor use Escape + d + W keys combination. To delete a line, use Escape + d + d keys combination. To delete multiple lines, use number of lines before dd command.

how to delete in vi command

How to undo / redo last command or action in Vi (Vim) editor

To undo last action or command use Escape + u keys combination. To undo all actions performed in current line use Escape + U keys combination. To redo last command use Escape + Ctrl + r keys combination.

Following figure illustrates undo / redo operation.

how to do undo redo in vi editior

I used dd command to perform the action for demonstration.

How to set line number in Vi (Vim) Editor

By default line numbers are not displayed. But if requires, we can display them with Escape + : +set +nu command.

Following figure illustrate this operation.

vi command set nu option

How to save and exit from Vi (Vim) Editor

To save and exit use Escape + : + w + q keys combination. To exit without saving use Escape + : + q + ! keys combination. We can also use Escape + : + q keys combination to exit, if file is not changed.

Following figure illustrates save and exit operation

how to save file in vim command

Vi / Vim Editor commands cheat sheet

Command Description
i Insert text before the current cursor position.
I Insert text in the beginning of the current line.
a Append text after the current cursor position.
A Append text at the end of the current line
o Open new line below the current line and switch in input mode
O Open new line above the current line and switch in input mode
/string Search for string in forward direction
?string Search for string in backward direction
n Select next search result
N Select previous search result
:%s/old/new Replace string at first occurrence in forward direction
:%s/old/new/g Replace string globally
yy Copy single line and put it in memory buffer
2yy Copy two lines and put them in memory buffer
yl Copy single character from cursor point and put it in memory buffer
yw Copy single word from cursor point and put it in memory buffer
p Paste contains from memory buffer one time
3p Paste contains from memory buffer three time
x Delete single character after cursor
X Delete single character before cursor
dw Delete single word from cursor (in forward direction)
dW Delete single word from cursor (in backward direction)
dd Delete single line from cursor
4dd Delete four lines from cursor
q Quit from file if it is not changed.
q! Quit from file without save if it is changed
wq Save and quit form file
u Undo last command
U Undo all change in current line
Ctrl+r Redo last command

Always press Escape key before typing any command otherwise whatever you typed will be inserted in file as a regular text.

That’s all for this article. In next article we will learn another topic step by step.

Download Download This Article

Share this Share This Article with Friends

Stay updateStay Update With US

More Articles For YouYou May Also Like