vi Text Editor

A text editor is one of the most important tools a Linux system administrator uses Because most of the services of a Linux computer are configured by editing an ASCII file. To edit these configuration files you need a text editor. There are a lot of text editors available in Linux, for example:

  • vi
  • emacs
  • xemacs
  • xedit
  • gedit
  • kwrite
The advantage of command line editors is that you can use them without having a graphical desktop environment installed. vi editor is the most important text editor on Linux systems

Start vi

You can start vi by entering vi followed by various options, and the name of a file to edit, as in the following example:

vi testfile

If a file does not yet exist, it is created. The text of the file appears in an editor at the command line:

The sign “~” indicates lines that do not exist yet. The cursor is on the first line.

Use the Editor vi

You can move the cursor with the k, j, h, and l keys (k one line up, j one line down, h to the left, l to the right) or by using the arrow keys (Up-arrow, Down-arrow, Left-arrow, Right-arrow).

Learn the Working Modes

When vi is first started, it is in command mode. Anything you enter in this mode is considered a command. You must switch to input mode before you can type any text. In addition to switching modes, you must learn which keys perform which actions because you cannot use the mouse. However, the number of commands needed for everyday work is fairly small, and you can get used to them quickly.

To enter text, you must first switch the editor to input mode by typing i (insert) or pressing the Insert key. At the bottom of the screen, you see the message --INSERT--.

Press Esc once to take you back to the command mode. From command mode you can switch to command-line mode by entering “:”. The cursor jumps to the last line after “:” and waits for a command entry.

A command will only be carried out in command-line mode after you press Enter. Then you are automatically back in command mode.

The following is a summary of the available modes:-

Command mode: When vi starts, it is automatically in this mode. In command mode, vi can be given commands. The command i puts it into insert mode and the command : switches it to command-line mode.

Insert mode: In this mode, vi accepts all input as text. Return to command mode with Esc.

Command-line mode: In this mode, vi accepts commands from the command line. Pressing Enter causes the command to be executed and automatically returns to the command mode.

You can use the following commands in command mode:-

Command Result
i or Insert : Switches vi to insert mode
x or Delete : Deletes the character where the cursor is
dd : Deletes the line in which the cursor
is located and copies it to the buffer
D : Deletes the rest of the current line from the cursor position.
yy : Copies the line in which the cursor is located to the buffer
p, P : Inserts the contents of the buffer
after/before current cursor position.
zz : Saves the current file and ends vi.
u : Undoes the last operation.
/pattern : Searches forward from the cursor position for pattern.
?pattern : Searches backward from the cursor position for pattern.
n : Repeats the search in the same direction.
You can use the following commands in command-line mode:-
Command Result
:q : Ends vi (if no changes were made).
:q! : Ends vi without saving changes in the file.
:wq or :x : Saves the current file and ends vi.
:w : Saves the current file.


Popular posts from this blog

How to create Oracle stored Procedures using TOAD for Oracle

How to Create Packages in Oracle Database using TOAD for Oracle

Create Tables in Oracle Database using TOAD