Brought to you by molecularsciences.org.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
This publication may not be redistributed without this notice.

VI editor

The VI editor is a screen-based editor. It is very powerful and has been around for more than a decade. Programmers are especially fond of the VI editor due to its powerful features to aid programmers. VI is often overwhelming for beginners. It takes some time to get used to VI but it is often well worth the effort. The key to mastering VI is to learn sequentially, getting used to the commands you learn at each step.

Starting the VI Editor

VI editor lets you create new files or edit existing files. The command to start is:

vi filename

Then you hit return. If the file exists, it will open for editing. Otherwise it vi will create a new file with that name.

At the bottom of your screen, you will see the filename and the filesize. Something like:

"filename" 234 lines, 3685 characters

Quitting VI editor

VI editor runs in two modes: edit mode and command mode. To quit VI, you need to be in command mode. If you are not in command mode, you need to hit the Esc key on your keyboard to exit the edit mode to the command mode.

To quit without saving changes, type:

:q!

To quit saving changes, type either:

:wq

or

ZZ

Then hit return.

Useful Commands

Following are some useful commands:

$ vi - this open a new file
$ vi somefile.txt - this opens somefile.txt. If it doesn't exist, it creates it.
$ vi /home/somesite/somefile.txt - unix addresses can be used

:ZZ - save and exit
:wq - save and exit
:w - save
:w >> filename - append to file
:q - quit
:q! - quit with saving
:e! - wipe all changes and return to original file

Modes

VI editor has two modes:

VI starts out in command mode. There are several commands that put the VI editor into insert mode: i, s, a, c, r, etc. Each of these commands is slightly different and they are discussed later. To get back to insert mode, you need to hit Esc.

Inserting and deleting text

To insert text, you need to switch from command mode to insert mode. You can do this by using one of the several commands listed below. For example, i allows you to add text to the left of the cursor, a allows you to add text to the right of the text, and so on. To get out of insert mode, you hit Esc.

List of Commands

i Insert before cursor
I Insert before line
a Append after cursor
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters

To delete you must you the commands: d or x. It must be combined with a movement command. Following are some command examples:

x Delete character to the right of cursor
X Delete character to the left of cursor
D or d$ Delete to the end of the line
dd Delete current line
:d Delete current line
dw Deletes a word
5dd Delete 5 lines
4x Delete 4 characters

Using vi for multiple files

You can work on multiple files using vi using a single terminal. Suppose I have three files:

- Experiment.php
- Spectrum.php
- Tag.php

To open all three files at once, I would type the following command:

$ vi Experiment.php Spectrum.php Tag.php

This would open all three files show Experiment.php on my terminal. To see the files which are open, I type the following command in vi:

:ls

This would list all the open files. To open yet another file, e.g. Peak.php, I type the following vi command:

:e Peak.php

To verify:

:ls

You should see the Peak.php in this list.

  1 #    "Experiment.php"               line 154
  2      "Spectrum.php"                 line 0
  3      "Tag.php"                      line 0
  4 %a   "Peak.php"                     line 1

A number is listed to the left of each file. This can be used to switch between files. To switch to Tag.php, type:

:bu 3

You can also switch back to the command line without closing any of the files. To do so, type ctrl-z To switch back, type fg.

Converting tabs to spaces

Tabs often breakup when the source code is moved between different system. In certain cases, tabs are not recognized and you get ugly looking symbols. The best way to avoid this frustration is to insert spaces instead of tabs. Obviously, you wouldn't want to hit the space tab 20 times on each line. The solution is to set the tab to insert predefined number of spaces instead of a tab. In vi, you can do this by setting expandtab as follows:

:set expandtab

To control the number of spaces, you still need to use tabstop as follows:

:set tabstop=4

To convert all existing tabs to spaces, use retab:

:retab

To set tab spaces inserted for indentation, use the shiftwidth as follows:

:set shiftwidth=4