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

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.

Autoindent Problem

Problem When you paste code from clipboard to vi, an extra tab is added for every line as following:

Line 1
 Line 2
   Line 3
     Line 4

Solution 1. In command mode, type the following to switch to paste mode:

:set paste
  1. Switch to insert mode and paste you text
  2. In command mode, type the following to switch back to nopaste mode:

    :set nopaste

Getting rid of ^M

Problem You copy pasted text from some editor and you ^M was inserted on every line. ^M is a carriage return in Microsoft Windows environment.

Solution Find out how you can reset your editor to unix character encoding. This way you will not continue to have this problem moving forward.

Open the document in vi and type the following in command mode

:s ctrl + shift + V ctrl + shift + M

This should appears as:

:%s/^M//g</

Hit return and the characters will be removed

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
Technologies: