Using VI Editor

VI Editor (Visual Editor)


You must be familiar with notepad in windows which is used to edit a file. Like-wise we have VI editor in UNIX, LINUX & SOLARIS OS used widely for editing files. 
However, Unlike notepad it is little tricky to use. I wish the VI editor would have been developed by Bill gates rather than Bill Joy
Anyways, guys we don't have any other option rather than getting aware of all these commands so that we become proficient in working with the VI Editor.


Understanding different modes in VI Editor:
There are three different modes in VI editors:
1. Command Mode
2. Insert/input Mode
3. EX mode

By default when you will open the VI editor, it will be in command mode. In the following sections we will see: 
1. How to switch from one mode to another?
2. What are the different VI Commands that we can use in these modes.
 

Command Mode : 
This is default mode of the VI editor. In this mode we can delete, change, copy and move text.

VI Navigation Commands:
Key
Use
j(or down arrow)To move the cursor to the next line (move down) 
k(or up arrow) To move the cursor to the previous line (move up) 
h(or left arrow) To move left one character
l(or right arrow)To move right one character
HTo move the cursor to current page beginning of the first line.
GTo move the cursor to current page beginning of the last line.
bTo move the cursor previous word first character
eTo move the cursor next word last character
wTo move the cursor to next word first character
^Go to beginning of line 
0Go to beginning of line
$Go to the end of the line
CTRL+Fforward 1 screen
CTRL+Bbackward 1 screen
CTRL+Ddown (forward) 1/2 screen
CTRL+Uup (backward) 1/2 screen
  
Copy & Paste:
KeyUse
y+w To copy rest of the word from current cursor position. 
n+y+w To copy n number of words from the current cursor position.
y+y To copy a line
n+y+y To copy n lines
p(lowerCase)To paste a copied words/lines after the current position of the cursor
P(uppercase)To paste a copied words/lines before  the current position of the cursor

Deletion:
Key
Use
x deletes a single character 
n+X To delete n number of characters from the cursor position in a line.
d+w To delete rest of a word from current cursor position
n+d+w To delete n  number of words from the cursor position in a line
d$Delete rest of line from current cursor position
DDelete rest of line from current cursor position
d+d To delete an entire line
n+d+d To delete n lines from current cursor position

Few More Important Command Mode VI commands:
Key
Use
uUndo changes (only one time) 
UUndo all changes to the current line 
~To change the case of the letter
ZZSaves the changes and quits the vi editor 



Input or Insert Mode: In this mode we can insert text into the file. We can enter the insert mode by pressing following keys in command mode:

Key
Use
iInserts the text before the cursor 
IInserts the text at the beginning of the line 
oOpens a new blank line below the cursor
OOpens a new blank line above the cursor
aAppends text after the cursor
AAppends the text after the line
rreplace the single character with another character 
Rreplace a entire line
EscTo return to command mode



 Last line mode or Collan Mode : This is used for advance editing commands. To access the last line mode enter ":" while in command mode.

Key
Use
:To get to collan mode(This need to be entered every time a user wants to use collan mode command)
:+set nu Shows line numbers
:+set nonu Hides line numbers
:+enter+n Moves the cursor to the n line
:+/keyword To move the cursor to the line starting with the specific keyword
:+n+d Deletes nth line
:+5,10d Delete line from 5th line to 10th line
:+7 co 32 Copies 7th line and paste in 32nd line
:+10,20 co 35 Copies lines from 10th line to 20th line and paste it from 35th line

:+%s/old_text/new_text/g Searches old string and replaces with the new string
:+q+! Quits vi editor without saving
:+w Saves the file with changes by writing to the disk
:+w+q Save and exit the vi editor
:+w+q+!Save and quit the VI Editor forcefully.
1,$s/$/" -type=Text_to_be_appendedAppend text at the end of the line




Using VI editor Command:
vi options <file name>
The options are discussed below:
-r : To recover a file from system crash while editing.
-R : To open a file in read only mode.

Viewing Files in Read Only Mode:
view <file name>
This is also used to open the file in read only mode. To exit type ':q' command.

Automatic Customization of a VI session:
1. Create a file in the user's home directory with the name .exrc
2. enter the set variables without preceding colon
3. Enter each command in one line.
VI reads the .exrc file each time the user opens the vi session.
Example:
#cd ~
#touch .exrc
#echo "set nu">.exrc
#cat .exrc
set nu
#
In the above example we have used set line number command. So whenever the user opens the vi session, line number is displayed.

I know its nearly impossible to keep all the above commands in mind, even I don't have, but as we keep practicing, we will be knowing most of them.

2 comments:

  1. Waaoow..beautifully written
    all topic are written in stepswise..i found this blog really helpful..thank you so much for the efforts..:)

    ReplyDelete