Introduction to vi editor in Linux and Unix system
#1

Introduction to vi editor in Linux and Unix system
The vi editor is the classic screen editing program for Unix systems and is based on a older line editor called ex. It can be used from any type of terminal because it doesn’t depend on arrow and function keys. It uses standard alphabetic keys for command as well as arrow keys.

There are number of enhanced version of vi exists. Some of them are nvi, vim, vile and elvis. In Linux, the vi command is a page link to one of these programs. Most likely it will be vim. Most of the Linux distributions have vi and emacs text editor already installed.

In Linux, configuration files are written in plain English. Hence, text editors are required to make changes to these files. The editing capabilities can be invoked within vi by pressing the colon (Smile, entering ex command and then pressing the Return key. ex commands are still important part of vi and is sometimes called hex in Linux.

Learning vi or emacs is must for every Linux or Unix user. There are some GUI based text editors available. But, there are some situations when you can’t access GUI desktop. In that case, vi or emacs can only save you.

The version which people use normally now a days is Vi iMproved (VIM). Vi was enhanced by Bram Moolenaar and are called vim editor.

The vi editor has three modes, command mode, insert mode and command line mode.

1.Command mode: by default, the vi editor opens in command mode. You can use commands to move cursor, delete. cut, copy, paste and save changes. Commands are case sensitive. The ESC key is used to end a command.

2. Insert mode: Insert mode is what we work in most of the time and text is inserted into the file in this mode only. You can enter the insert mode by pressing “I” key. The newer vi versions will show “INSERT” word on the bottom line. You can also enter "a" (insert after), "A" (insert at end of line), "o" (open new line after current line) or "O" (Open line above current line) commands apart from “I” command. The ESC key ends insert mode and returns you to command mode.

3.Command line mode: You can enter this mode by typing ":" which displays the command line entry at the bottom of the screen.

Note: In broad sense, there are only two modes, one for editing text (i.e. Insert mode) and the other (i.e. command mode) for giving commands. To switch between the two modes you use the “I” and “Esc” keys.



Starting vi

1. To start Vi, open a terminal or console and simply type vi filename (for existing file) or vi newfile (newfile will be name of your new file)

2. The file will open in command mode.

3. Type “I” to go to Insert mode and add your text in the file.

4. Once you are done editing the file, Press “ESC” to go to command mode and save your changes to the file. Press “Shift+ZZ” to save the file. Alternatively, you can use command line mode to issue command to save the file.

Swap file:

Swap files has .swp extension. Swap file is automatically created when you make mistake in saving file. Mistake like issuing wrong command CNTRL+ZZ instead of SHIFT+ZZ. It is similar to temp files in Windows or DOS. .swp file will be in addition to your original file. In this case, the original file will not contain the recent changes.

Command to recover from swap file: Type the following command at the $ prompt and press ENTER

vi -r {file name}

You must delete the swap file after recovery: Type following command at the $ prompt and press ENTER

rm .{file name}.swp


Moving the cursor: This command only works when you are in command mode. The cursor is controlled with four keys: h, j, k, l.

h – one space left

j – one line down

k – one line up

l – one space right

Note: If you were in Insert mode, Press ESC to go to command mode.

Basic vi editing command
You must be in command mode to execute editing commands. Many of the editing commands are case sensitive. They have different meaning depending on whether they are typed as lowercase or upper case. Also, editing commands can be preceded by a number to indicate a repetition of the command.

Deleting characters:
1. X : will delete the character before the cursor
2. 4x : This will remove four characters

Deleting Words: Make sure to position the cursor to the first letter of word
1. dw : it will delete the word and space follwing it.
2. 3dw : will delete three words

Deleting Lines: Make sure the cursor is at the beginning of the line
1. dd : delete the entire line.
2. 2dd : delete two lines
3. D : To delete from the cursor postion to the end of the line.

Replacing Characters: This will replace one character with another
1. Position the cursor to the character to be replaced.
2. Then type r
3. Type the new character you want to replace with.
4. Now, the new character should appear and you should still be in the command mode.

Replacing word: move the cursor to the start of the word you want to replace
1. cw : vi will change itself to Inster mode and the last letter of the word to be replaced will turn into a $.
2. 3cw : will replace three words
3. Press “ESC” to get back to command mode.

Replacing Lines: This will change text from the cursor position to the eend of the line.
1. Type C
2. Type the replacement text
3. Press “ESC”


Inserting Text:
1. Position the cursor where you want the new text to appear.
2. Type “i”
3. Now you can start entering text
4. Press “ESC” to go back


Navigating a file:

w : forward word by word
b : backward word by word
$ : to end of line
0 : to the beginning of line
H : to top line of screen
M : to middle line of screen
L : to last line of screen
G : to last line of file
1G : to first line of file
CTRL + f : scroll forward one screen
CTRL + b : Scroll backward one screen
CTRL + d : Scroll down one half screen
CTRL + u : Scroll up one half screen


Search Text:

While in command mode,
1. Type /
2. Enter the text you want search
3. Press “Enter” Key
4. The cursor moves to the first occurrence of the text you typed above after slash
5. Type n to search in forward direction and N to search in backward direction

Saving and Closing the file:

Save the file and quit vi: Shift + ZZ

Save only and leave the file open:
1. Press ESC
2. Type :w
3. Press “Enter”

Don’t save and quit vi:
1. Press ESC
2. Type :q!
3. Press “Enter”
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: editing** cleanness in marathi, vi editor in linux, abstract of ppt on linux operating system, introduction for sunflower and legume pollination, university surrey unix, unix 4sem questions paper vtu, seminar on introduction of biomechatronics system,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  Study the working of RS flip-flop using NAND gates and NOR gates and compare them seminar class 0 5,113 13-05-2011, 04:36 PM
Last Post: seminar class
  Study the working of basic gates like AND gate, OR gate and NOT gate seminar class 0 2,292 13-05-2011, 03:50 PM
Last Post: seminar class
  INTRODUCTION TO CODE COMPOSER STUDIO seminar class 0 1,491 06-05-2011, 03:47 PM
Last Post: seminar class

Forum Jump: