Introduction to vi editor

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 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 (:), 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.
 
official site for vim: www.vim.org


No comments:

Post a Comment

Commonly Used Abbreviations Used Daily

Although, we tend to use short form of long words in daily life but there are many abbreviations which we don't remember. Below is the l...