The vi Editor

The vi Editor


Introduction

vi is a powerful editor with many features. It can handle very large files much easier than a program like Microsoft Word. Unlike Word, vi is only a TEXT EDITOR and you cannot include graphics or fancy fonts in your file.

Although there are lots of different commands in vi, it is possible to get started using vi knowing only a small subset of them. As you feel more comfortable with vi, you can learn the more advanced features.

vi has 3 modes:

  1. write mode, used for entering text
  2. command mode and
  3. command line mode, used for entering commands to vi.

Remember, in vi, the mouse cannot be used to issue editor commands or move the cursor.

This is also similar to the GCG sequence editor SeqEd, which has a command mode and an editing mode. It's best to use programs like GCG's SeqLab Editor to edit the sequences instead of using vi or a Word Processor. If you do edit a GCG sequence, then run the program reformat before using the sequence.

Write Mode

When you first enter the editor, you are in the command mode. To enter the write mode, type the letter a for append. This is one of the four possible commands for entering the write mode. vi is Case Sensitive. Lower case commands are different from upper case commands.

Command Mode

You are in command mode whenever you hit esc to leave the write mode. In command mode, you can move the cursor anywhere in the file.

The x key deletes individual characters, while dd deletes an entire line. To enter the insert mode type the i key. When you're done inserting, hit the "esc" key to return to the command mode.

Command-Line Mode

Command-line mode is used for such things as writing changes and exiting the editor. To enter command-line mode, type : while in command mode. The : will now appear at the bottom of the screen and the command which you type will appear on that line.

1. How to create a new file or modify an existing file:

EXAMPLE:

% vi practice

In this example, the new file "practice" is created if it doesn't already exist.

The screen will look like this after opening a new file entitled, "practice".

=============================================================
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"practice" [New File]
=============================================================
 

The Edit Mode

Once in vi, you are placed in the command mode. You cannot enter text in this mode. If you try, the letters you try to type will not appear on the screen. This is because ordinary letters are interpreted as commands.

You must enter the edit mode to type text.

To do so, use one of the following commands. The vi text editor is case sensitive, which means the same letters in upper and lower case have different command responses.

2. How to add information to a file:

What to Type

Result

a

append after the cursor

i

insert before the cursor

The "esc" key must be used to end any of the above commands or to begin any of the following commands (depending on which way you look at it). You must also hit the "esc" key prior to any cursor movement. When in doubt, hit the "esc" key and proceed.

3. How to delete information from a file:

What to Type

Result

x

deletes the character the cursor is under

dd

deletes the line the cursor is on

4. More edit commands

In the command mode, it is possible to move the cursor up, down, left or, right by using the arrow keys on the keyboard. However, no matter what type of keyboard you have, it is possible to use the vi commands, h, j, k, and l, all in lowercase, to control the cursor.

These commands move the cursor as follows:

What to Type

Result

j

moves the cursor up one line

k

moves the cursor down one line

h

moves the cursor backwards on a line

l

moves the cursor forward on a line

 

To cut and paste, I usually use the Mac commands instead of the vi commands. These work with NCSA Telnet. Just select the text to copy with your mouse, then move the cursor using the arrow keys (not the mouse) over to the area you want to paste the text, then make sure you are in the "insert" or "append" mode and then paste the text using the Macintosh command (Command-v).

5. How to save your work and exit from vi

First you need to enter the command mode by pressing the Escape key.
Now you are ready to give any one of a number of line commands.

What to Type

Result

:wq

saves the current changes and exits vi

:w!

saves the current changes but does not exit vi

:q!

exits vi without saving any change

6. More command lines

Other popular command include

 

What to type

What it does

ndd (for example 3 dd)

Deletes n number of lines

^

Go to start of line

nY

Copy n number of lines

$

go to end of line

:1

goes to top of file

:5

goes to fifth line of file

:$

goes to bottom of file

:set nu

will number all your lines

:set nonu

turn off line numbering

:1,$s/oldtext/newtext/g

Global substitutions

There are many different vi tutorials out on the internet, if you need more help, check out some of these.

Cornell Theory Center Tutorial
UCS Tutorial
Command List
vi Quick Reference

Or you can check out the man pages for vi


UNIX

Commands

Directory

File

Other

Help

Search