Home » 2014 » March » 3 » Working with VI Editor: Basic and Advanced

7:50 PM
Working with VI Editor: Basic and Advanced
This article will get you acquainted with one of the most useful and frequent editor in Linux system. You can learn the basic commands to work with VI Editor.
 Inside Vi Editor, how we do the editing, saving, deleting etc.

  • To open a file with vi editor, use vi filename . If filename exists it will open in edit mode and if its not present, an blank page will open with file name at the bottom.
  • Now inside editor, there are three modes of working:
    • Insert mode (press 'I' to enter into insert mode, now the keys are displayed on screen as data)
    • Escape Mode (press 'Esc' to come back to escape mode)
    • Command mode (all inputs are accepted as command)
Modes of operations of VI editor                    


  • Once you open the vi editor, press 'I' to enter the insert mode. Herein this mode you will be able to edit the file.
  • Once you typed some thing, press 'Esc' to exit Insert mode. Now editing is off.
  • :q -->Now press ':' and 'q' to quit witout saving.
  • :wq --> To quit Vi Editor with save, press ':wq' and press Enter
  • Suppose you have made any changes and you dont want it to be saved, press ':q!'  and Enter.

Search using pattern
  • /search --> If you want to search for a string in a large file, type '/keywrd' in escape mode. If the file contains that keyword, it will be highlighted. Press 'n' to go to next word found and so on..
  • Search will work only in escape mode. If you type '/keyword' in insert mode, it will be simply typed on the editor.
  • While doing searching, type '/' and press 'UP' and 'Down' keys to find the recent searc h keywords used.

To position the cursor on a line number in a file, we can try this:
 vi +[num] filename
Eg. Promt:> vi +4 abc.txt
The above command will put the cusrsor to the 4th line in the file abc.txt

Find and replace
To find a word in the file and replace it with another word, we can give commands inside VI editor directly. For this we first open the file in vi editor then we give ":" followed by below command:
Prompt:> vi abc.txt


Eg. :%s/old/new/g

The above command will find "old" and replace all occurances of "old" in the file by "new"   But this command will be case sensitive. So next we will see the case insensitive word match and replacement.


Eg. :%s/old/new/gi

The above command will search for the word "old" in case-insensitive manner and will replace the words old, OLD, Old, or any other permutations by the new word new.

The same if you want to be done after a confirmation you can try:


Now the system will ask you before doing the replacement like below. Here you can give "y" to say yes, "n" to say no "a" for all "q" to quit and so on..

replace with this (y/n/a/q/l/^E/^Y)?

Suppose you want to do substitutin in a certain range of file, then you have to specify start and end no. of lines of file where find and replace will take place.

:{START n},{END n}s/find-word/replace-word/gi

Eg.  :5,10s/old/new/gi

The above command will replace any occurances of old by new from the opened file only in the range of line no. 5 to 10 in the file.

Copy, paste, delete, undo and other editing inside VI editor

  • To copy one or more lines from a file, we can use yank method.
  • Go to the begining line where you want to start copying, (you should be in command mode or press ESC to go to command mode) press n (n is the numbers of lines you want to copy, say 3, to copy three lines)  and then press "yy".
  • So, it will copy n lines to clipboard. You will see a message: "n lines yanked" at the bottom of VI editor.
  • Now, go to the line where you want to paste the copied lines, and just type p (in command mode only).
  • The lines will be copied starting from there.
  • If you want to delete a line , just type "dd" in escape mode, the line will be deleted. You can use ":d" also to delete a line in command mode.
  • To delete n number of lines, go to the start line of deletion, type n (n= number of lines to be deleted) and "dd" in escape mode.
  • n lines will be deleted.
  • But if you want to delete lines in a given range, say, i want to delete lines from 10 to 20, then use below command:

:<startlineno>,<endlineno> d


:10,20 d

  • To undo your last action, use ":u" in command mode. Suppose you have deleted a line, now you want to undo. Give ":u", the lines deleted will re-appear.

Delete one or more words in VI editor

To delete a word in vi, put the cursor at the first character of the word and press 'dw'. It will delete the word starting at the cursor position.

To delete more than one words, press 'dNw'. Here 'N' referes to the number of words to be deleted. Example: d5w will delete 5 words staring with the word at cursor postion in the same line or from next lines.

If you dont place the cursor at first character of the word, the entire word will not be deleted. Only the characters starting from the cursor position will be deleted.

Example: If the cursor is at 'e' in the below line and you press 'dw', it will delete 'enius' not the entire word genius.

I am a genius.

Output :

I am a g.

Numbering the lines of a file

Suppose you want to number the all the lines of a file. You can use the "set -nu" command inside VI editor. Open The file by VI editor, give ":" followed by below command:

:set nu

The above command will a numbering to all the lines starting from 1, 2, 3.. and so on..

To remove numbering from the file, we can use below command:

:set nonu

Setting environment as VI editor.

The below command is used to set the Linux environment as VI editor. When we press UP/Down buttons, we can see recent commands.

set -o vi

Reading from another file into the current VI editor.

:r filename

This command will read from the file name mentioned and append the content in VI editor

Writing to a seperate file from vi editor

If you want to save or write the current content of vi editor to a seperate file, you can use ":w" inside vi editor.

:w somefile

The above command will write everything present in vi editor to a seperate file as mentioned.

  • Now suppose you want to write some selected lines or a range of lines from the vi editor a file, for that purpose use the start and end line number.
  • The the content which lies in the range will be written to a seperate file.

:<startlineno>,<endlineno> w somefile


:3,9 w somwfile

  • The above command will write the lines lying beyween line no. 3 to 9 to the filename given.

Splitting inside VI editor

Suppose you have one window open in vi editor and you are working with a file or you are editing a file. Now, you want to open another file in the same window. You dont want to close the previous file

You can open another file while previous file being opened. by splitting your vi editor. You can split your vi editor in two or more section to open more than one files in one go.

You have one file open. Now use below command to open another new or existing file:

:split newfile

This will split the editor in two sections and now you have newfile opened at the top and the previous file at the bottom. See below figure:

  Split current window in two.  The result is two viewports on the same file.  Make new window N high (default is to use half the height of the current window).  Reduces the current window              height to create room (and others, if the 'equalalways' option is set and 'eadirection' isn't "hor").

Note: CTRL-S does not work on all terminals and might block further input, use CTRL-Q to get going again.

               Also see |++opt| and |+cmd|.

VIM editor: open mutiple files in one go

To open several files in one go, we can vim instead of vi.

Lets say, I want to open file1, file2, file3 in one go, i can use below command at the prompt

vim file1 file2 file3

so it will open all three files, to jump from one file to anoter, use ":n" inside vi editor.

To open multiple files in several windows

Now the requirement is I want t open two or more files in seperate windows either vertically or horizontally inside a vi editor.

set -o[N] file1 file2 file3...

-o[N]       Open N windows stacked i.e. horizontally one over another.  When N is omitted, open one window for each file.

-O[N]       Open N windows side by side i.e. vertically. When N is omitted, open one window for each file.

  • To move from one file to another use ":n" in command mode.
  • To quit the file one by one use ":q" and to quit all files in one go, use ":qa".

To enable/disable mouse inside vi editor

Many times for editing, we face prolems in vi. We may need to enable or disable mouse to select texts for copy/paste/edit. 

To enable mouse:

:set mouse=a

Once mouse is enabled, you will enter into visual mode and see a mouse pointer inside vi editor. You can use that pointer to select texts for easy editing.

To disable mouse again:

:set mouse=i

The mouse pointer is gone, back to normal mode.

To open a file with the first match of a keyword

vi +/search-key file



Category: Open System-Linux | Views: 2338 | Added by: shanky | Tags: editor in Linux, working with vi editor, vi editor, vim, Vi | Rating: 5.0/1

Related blogs

You may also like to see:

[2014-12-28][Open System-Linux]
Shift command with examples
[2015-07-18][Open System-Linux]
Creating and Managing Logical Volume Manager in Linux
[2016-05-24][Open System-Linux]
FACTER command in Linux : showing system facts
[2016-05-11][Open System-Linux]
An example to understand bash exit code $? in linux.
[2014-09-20][Open System-Linux]
Export command in Linux

Total comments: 1
1 shanky • 4:28 PM, 2014-09-13
To get help for any command inside vi editor, use ":help <command>"

For example, to get help for split command, use
:help split