Brief introduction to the vi Editor

The use of VPS/VDS (Virtual Private Servers or Virtual Dedicated Servers) on the Internet is becoming quite popular, and the large majority of these run Linux distributions.

The normal, day to day administration is done through browser based interfaces, such as Plesk, however, you also can use shell access to administer the server, where you can have total control over the server via a secure SSH connection using a program such as Putty. (available for download free from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html)

This type of server administration is also very popular, allowing for work to be done remotely across the internet in a safe way.

Putty’s normal use is text based command line administration, although remote graphical interface administration is also possible.

Definitely when you are administering a Linux (or UNIX) server remotely through a command line interface you will need to use a text editor. Linux comes with a wide range of text editors, however, the most popular are vi and emacs. This article gives a very brief introduction to vi.

The vi editor was developed in 1976 by Bill Joy at the University of California. An enormous amount of text based and graphical based editors came out from that time, however, the vi is still very popular amongst the Linux / Unix users.

To open vi there are several ways. The simplest is to type ‘vi’ and press enter. This opens vi and creates a new file. The second way of opening vi is to type ‘vi filename’ where filename is the name of the file that you want to edit. This opens vi, and then opens the file you want to edit in it.

When you open vi, you will have a box at the top, representing the cursor. The line in the bottom will give you useful information regarding what operation you are currently doing. Empty lines are marked with a starting ‘~’ character.

There are two operating modes, command and input. When started, vi will be running in command mode. In command mode, vi will consider your input as commands, and when in input mode, vi will consider what you type as text in the file. Always remember that you are working in a case sensitive environment – heLLo is different from Hello.

Brief introduction to the vi Editor

To start inserting text, you can use the following two commands:

  • ‘a’ – Add command. Text will be inserted to the right of the current cursor position
  • ‘i’ – Insert Command. Text will be inserted starting from the left of the current cursor position.

Pressing the ‘Esc’ key will take you back into the command mode once you are ready inserting/editing the text in the file.

To exit from vi, use the following commands (remember to press the ‘esc’ key first to go back to command mode) :

  • ‘ZQ’ – Exit without saving the changes
  • ‘ZZ’ – Exit and save the changes

To go around in a document, go in insert mode and use the arrow keys. Most versions of vi also support the PgUP and PgDOWN keys to scroll multiple pages.

To delete text, in insert mode use the normal ‘DEL’ and ‘Backspace’ keys. In command mode, you can use the following:

  • ‘x’ – deletes the character under the cursor
  • ‘X’ – deletes the character before the cursor
  • ‘dw’ – deletes from the current character to the end of the current word
  • ‘d$’ – deletes from the current character to the end of the current line

Other useful commands:

  • ‘u’ – Undo previous change
  • Ctrl+R – Redo previously undone change

To search for text:

  • ‘/example’ – Searches forwards from the cursor position for the word ‘example’
  • ‘?example’ – Searches backwards from the cursor position for the word ‘example’

Other resources

http://www.cs.rit.edu/~cslab/vi.html provides a list of commands available for the vi editor.