Using the vi Editor On Linux

Linux relies on large number of text files for configuration. So, you need a text editor to configure Linux. vi is one of the most commonly used text editor.

It is easy to open a file with vi. For eg:- If you need to open /etc/inittab file, use

# vi /etc/inittab

There are three basic ways to work in vi

  1. Command mode is default
  2. Insert mode is to insert text.
  3. Execute mode to run regular shell commands

Command Mode:-
When you open a file in vi, first or default mode is command mode. It can be used to scroll through text, search for different text strings delete specific characters, words or lines. One aid in vi is line numbers, which can be activated as

:set nu

1. open terminal and type  
# vi /etc/inittab

2. vi opens inittab file as follows

3.Now to see the line number type :set nu

4.you get



vi allow you to use the directional keys on keyboard (arrows, page up, page down). If you know the line number you want, the G command can help you. When used with line number, such as 20G, it takes you to desired line.

DELETING TEXT:-
There are three commands associated with the current location of the cursor:

     :       Deletes the current character
dw   :       Deletes the current word
dd    :       Deletes the current line

If you accidentally deletes something, than U command reverses the last command entered.

SEARCHING FOR TEXT:
Just start with a forward slash ( / ) followed by the word. For example;

/proceed
Cursor highlights the first place where the "proceed" is found in the file.

INSERT MODE:-
It is used to insert the text into a file. These are several ways to do this as follows:

      COMMAND
     ACTION
       COMMENT
           i
     Insert
 Everything type is inserted, at current position of cursor
           a
      Append
Everything type is inserted, starting one character after current cursor position.
           A
      Same as a
Closely related to a, where everything is inserted, starting at the end of the line with cursor.
           o
       Open
Everything type is inserted, starting one line below current position of the cursor. If "O" is used it insert , it starts one line above current position of cursor.

Execute Mode:-
Regular shell commands can be run from inside the vi editor.  Just type " :! " followed by the command.

For example;
If you are creating a cript, you will need to know the directory location of a certain file. You list the files in /etc/cron.daily directory as:-

:!ls /etc/cron.daily

Regular execute mode starts with colon ( : )
File management are associated with execute mode, including :q(to exit a file) and :w(to write current text to file)
If you think there is some problem in text you inserted, exit from vi without saving any changes, use the :q command.

Related posts:-

Comments

1 Response to "Using the vi Editor On Linux"

Picastana said... July 15, 2009 at 4:58 PM

Great Post on Linux

Post a Comment

 

Managing Shell And its Secrets

Subscribe To Get Update On Mobile

Subscribe
Get updates of latest post on your mobile free