vi editor 


"vi" is the screen based editor which come by default with Unix operating system. Bill Joy created this editor in 1976 for BSD system. We can also term it as "Visual editor".


Another extension if "vi" is "vim" which is "vi improved", All the basic operating syntax used in "vi" can also be used in "vim". There are other popular text editors in Unix as - "pico", "GNU nano", "ed", "ex", "Emacs" etc. but "vi" is the most popular editor till the date. The reason behind this is, it is almost available in all flavor of Unix. It follows the same set of implementation standard across the globe with less resources. it is more user friendly and efficient than other text editors like:"ex"and "ed".


"ex" is line oriented Unix editor, yet in different interface but it is possible to switch back and forth during text editing in "vi". Another, "view" is also considered to be the part of same program. It is "read-only" editor and can be used to open the files.






Starting vi editor:


  • vi filename : This will create a new file if file is not present. If file is already created  then it will open a file for text editing. 


$ls -ltr
total 12
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
drwxr-xr-x  2 pinku  guest  512 May 11 13:05 A2
-rwxr--r--  1 pinku  guest    3 May 14 10:02 file1.txt

$
$vi Test.txt


:wq (To save and exit)

$

$ls -ltr
total 12
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
drwxr-xr-x  2 pinku  guest  512 May 11 13:05 A2
-rwxr--r--  1 pinku  guest    3 May 14 10:02 file1.txt
-rw-r--r--  1 pinku  guest    0 May 14 10:03 Test.txt
(New file created)


  • view filename : To open file in "read-only" mode, the file which is already created. 


$view Test.txt


:wq

Read-only file, not written; use ! to override.

:q

$


  • vi -R filename : Same as view, start editor in read-only mode. 


$vi -R Test.txt


:wq
Read-only file, not written; use ! to override.
:q
$


How to Quit from editor:


  • press ":q" (quit) or ":wq" (save and quit) then press Enter key.
  • press "Shift ZZ" to save your work and quit. 


Quit from editor will take you to the shell and default prompt "$" will be visible. 



There are 3 operating modes :​​


  • Command Mode : It's a "default mode". When we open an editor using "vi", we will be in this mode. Here we press some set of keys to give some set of instruction / command to the editor for text editing. We can switch back from other mode ("Input-mode") by pressing escape key.


The major drawback you can say that it will not display anywhere that we are in "Command mode" or "Input mode" So for checking, if you press escape key more than once then it will sound beep for each one. It indicates that you are in command mode.


  • Input Mode : This mode is basically for inserting text in files, also called "Insert mode". We can enter into this mode from command mode by pressing some set of keys like : i (insert), a (append) etc. For switching back to command mode, press escape key.


  • "ex" Mode : This is also called "Last line mode" or "Colon mode". The way we invoke this mode is by pressing ":" colon symbol from command mode. When we give one line "ex" mode-command followed by "Enter" key, we automatically switch back to default command mode. So we have to press ":" colon every time whenever we give "ex" mode command.















Just we are going to start with commands for "default mode" and "ex mode", one by one. "Input mode" is used only to enter text in a file so there are no commands for this mode.








"Command Mode" Commands:


CommandsActions 
Page Navigation by moving cursor:
hmoving cursor left
jmoving cursor down
kmoving cursor up
lmoving cursor right
wmove the cursor at the beginning of the next word in forward (right) direction
emove the cursor at end of the word in forward direction
bmove the cursor at beginning of the word in backward (left) direction 
0 "zero"move the cursor at the beginning of the line
| "pipe"move the cursor at the beginning of the line
$move the cursor at the end of the line
shift G move the cursor at the end of the file
shift Hmove the cursor at the beginning of the page
shift Mmove the cursor at the middle of the page
shift Lmove the cursor at the last line in page
(move the cursor at the beginning of the sentence
)move the cursor at the beginning of the next sentence in forward direction
{move the cursor at the beginning of the paragraph 
}move the cursor at the beginning of the next paragraph in forward direction
Scrolling a page
Ctrl fscroll the full page in forward direction
Ctrl bscroll the full page in backward direction
Ctrl dscroll the half page in forward direction
Ctrl uscroll the half page in backward direction
Text Editing
iInsert mode, Insert text in the backward direction from cursor position
shift IInsert mode, Insert text at the beginning of the line
aappend mode, append text in the forward direction
shift Aappend mode, append text at the end of the line
oCreating a blank line under cursor position
shift OCreating a blank line above cursor position
uundo command, rollback to last change only 
shift Uundo command, rollback to multiple changes
cccut option, cut the line from file
cwcut option, cut the characters from current cursor position to the end of the word in forward direction
rreplace command, replace a single character with another single character in cursor position
shift Rreplace command, replace a single character with multiple characters in cursor position and existing text are overwritten
ssubstitute command, replace a single character with multiple characters in cursor position
shift Ssubstitute command, replace entire line
xdelete character, delete a character under cursor position in forward direction
shift Xdelete character, delete a character just before the cursor position in backward direction
dwdelete word, from current cursor position to the end of the word
dbdelete word, from current cursor position to the beginning of the word
dddelete current cursor line
d$delete from current cursor position to the end of the line
shift Ddelete from current cursor position to the end of the line
d |"pipe"delete from current cursor position to the beginning of the line
d 0"zero"delete from current cursor position to the beginning of the line
d shift Gdelete from current cursor position to the end of the file
ywcopy the word under cursor position
yccopy the character under cursor position
yycopy the line under cursor position
pPaste a copied line under the cursor position / paste part of the text in forward (right) direction from the cursor 
shift PPaste a copied line above the cursor position / paste part of the text in backward (left) direction from the cursor 
shift Jjoining two lines
Other Commands
. "dot"repeat the last command
shift ZZSave the file and exit from "vi" editor




































































































"ex-Mode" Commands :


Just after giving the below commands and press enter, we will be in "command mode", so press ":" to give another one line command in "ex mode".



CommandsActions
:qquit from editor (without save)
:wsave the changes in file
:wqsave the changes and quit from editor
:q!quit forcefully without save 
:xsave and quit
:1,4w!save only first 4 lines
:1,$w!save the changes from 1st to last line in file
:%w!save the changes from beginning to end of file
:w filenamesave the changes with given file name
:W! filenameoverwrite the given file name with the open contents of file
:5w filenameWrite 5 lines from current cursor line to the given filename.
:$wsave the last line
:.,$wsave from current cursor line to the last line 
 :1,3t4copy from 1st to 3 rd line and paste it on 4th line 
:1,3m4cut from 1st to 3rd line and paste it on 4th line
:e! filenameopen a given file for editing
:! ls -lrun the given command without quitting editor
:set nuall the lines in file will be displayed with line numbers
:set nonuline numbers will be removed from lines in a file
:set allshows all the options with an editor
:shyou can go to shell without quitting an editor
:=count the total number of lines in a file
:.,=count the number of line from beginning to current cursor line




































Pattern search with vi Editor:


This is done in "ex mode" by giving line command with ":", we can search the pattern in following two ways:


  • Forward direction: By giving the command, it will search the given pattern from beginning of the file in forward direction. When the pattern is found cursor will be blinking at that point.


:/pattern (press Enter)

press "n" or "shift N" to move forward in file


  • Backward direction: By giving this command, it will search the pattern from last line of the file in backward direction. 


:?pattern (press Enter)

press "n" or "shift N" to move backward in file



Substitute Command:


Here first it searches for the pattern and then replace it by the given string. It operates on regular expression as well given in the pattern. This command works in "ex" mode so it is necessary to start with ":" colon at the beginning of the command. Another, it comes to "Input mode" after performing substitute command.


Syntax:

:[range] s/pattern/string/[option]


Where,

  • "[range]" is optional, here we can mention numbers as well as shells meta-characters
  • "s" stands for substitute, and "/" is the separator. "#" can also be used like - "[range] s#pattern#string[option]" in this place but its used is not so popular in Unix.  
  • we mention some options like "g" (globally) or "d" (delete) at the end which is also optional and used when required. 


Examples:


$vi worker.txt
1  Parker            04264554792
2  Peter Paarker     04653476238
3  Blaze
4  Braain
5  Dorothy           4768256981
6  Linda
7  prker

~

~

~

:1,$ s/Braain/Brian/g 


$vi worker.txt
1  Parker            04264554792
2  Peter Paarker     04653476238
3  Blaze
4  Brian
5  Dorothy           4768256981
6  Linda
7  prker


Similarly, to delete blank lines in a file:


$vi Cocoon

One day, a man saw a cocoon.

He loved butterflies and had a craze for its wonderful combination of colours.

In fact, he used to spend a lot of time around butterflies.

He knew how a butterfly would struggle to transform from an ugly caterpillar into a beautiful one.

~
~
~

:g/^$/d


​$vi Cocoon
One day, a man saw a cocoon.
He loved butterflies and had a craze for its wonderful combination of colours.
In fact, he used to spend a lot of time around butterflies.
He knew how a butterfly would struggle to transform from an ugly caterpillar into a beautiful one.


Deleting Blank spaces from a file:


​:1,2 s/ //g


$vi Cocoon
Oneday,amansawacocoon.
Helovedbutterfliesandhadacrazeforitswonderfulcombinationofcolours.
In fact, he used to spend a lot of time around butterflies.
He knew how a butterfly would struggle to transform from an ugly caterpillar into a beautiful one.


Questions & Answers:


Qs: what, if we give only "vi" command without a filename?


It will open the vi editor with no file names, giving the message at the bottom of the screen:


/tmp//vi.eXPkqHs3DX: new file: line 1


we can edit the file and afterwards we can give the name to this file. 


How to save this new file?


:w! filename [Enter]

It will not exit from "vi" so after editing file, quit by giving command

":wq [Enter]"


Qs: Give the name of some other popular editors which can be used in Unix ?


  • vim (vi improved)
  • pico
  • GNU nano
  • ed 
  • ex
  • Emacs
  • sam
  • BBEdit
  • JED


why "vi" editor is so popular?


There are various reason for this, but major reasons are: it is free, coming with the package and useful while creating small files. It is very rich in commands. Commands are really short which makes it faster to use. We can use regular expression to perform pattern search.


Qs: How to move to 15th character at the current cursor line ?

15| (pipe)


Qs: How to go to 5th line in a file?

5G


Qs: Copy the current line and paste it in 10th line.

yy10Gp


Qs: Copy the 1st and 2nd line and paste it in 5th line.

1G2yy5Gp (Command mode)

:1,2t5 [Enter] (ex mode)


Qs: Delete 5 characters under cursor position in forward direction.

5x


Qs: Delete 5 lines in forward direction.

5dd 


Qs: In how many ways we can quit from vi editor ?

  • :q [Enter]
  • :q! [Enter]
  • :wq [Enter]
  • :x [Enter]
  • shift ZZ in command mode


Qs: How to set line numbers in vi editor?

:set nu [Enter]


Qs: How to count total number of lines in vi ?

:= [Enter]


Qs: How to delete blank lines and blank spaces from a file?


:g/^$/d [Enter] - Delete blank lines

​:1,2 s/ //g [Enter] - Delete blank spaces


Qs: without quitting vi editor how to go to shell prompt?

:sh [Enter] - ex mode

Ctrl z - command mode


Qs: How to open another file without closing the current file in vi ?

:e! filename [Enter]


Qs: How to list out files in editor without going to shell prompt ?

:!ls -l [Enter]



NEXT->

Cracking IT Interview