Minimal vi

 

The proficient use of the vi editor is essential to working with any version of the Unix operation system.  Learning the vi editor can be confusing because it includes so many commands, but in reality, no one uses or needs all of them. In fact, it is possible to make any edit to any file using just 12 commands of the vi editor as long as you understand how the editor works.

 

Our goal is to end up with a basic understanding of what vi is and how it works and also to be comfortable using just those vi utilities that are absolutely essential. Once you have done that, you can add other utilities to your repertory as your needs require.

 

Once you have invoked the vi editor, you can completely edit any file using the arrow keys and just eight other keys: a, i, w, q, :, x, RTN (or ENTER) and ESC.

 

The Nature and Structure of vi

 

The vi editor uses space in the Random Access Memory (a “buffer”) to store and display file data, and it uses a specific set of tools to change displays and enter commands into the utility.

 

vi: An editor, not a word processor

 

The vi editor is a screen based editor, which distinguishes it from Word or Wordperfect, which are GUI-based word processors. Word processors include two features - an editor and a formatter: vi is just an editor. You can insert formatting statements into vi for use with a formatting utility such as troff or latex, but vi itself will not format your files. This implies three important things:

 

1)    Files created with the vi editor are simple ASCII text files. They do not have to be opened with any particular application and can be imported into many applications. A file created with vi can easily be imported into Word, can be edited with Unix's older text editors ed and ex, can be edited with Xwindows desktop editing applications, and can be understood by the Unix kernel.

2)    You will have to insert your own carriage returns at the end of each line, and if you import your file into a word processing application, you will have to remove the end-of-line marks (hard returns) from each line.  The vi editor will create lines of only 255 characters, so you cannot ignore ends of lines - eventually you will have to hit return or you will cease to be able to add to your file.

3)    The vi editor is independent of the desktop. It uses no GUI and does not recognize the mouse. Therefore, all movement within the file being edited, all changes to existing text and all movement between editing and insertion modes is controlled by the user with sequences of keystrokes. It is this third characteristic of vi that makes it difficult for the novice, who is used to moving around a file with a mouse.

 

 

The Three Modes of vi

 

When you enter the vi editor, you are automatically placed into the "command" mode of vi. The vi editor has three modes: command, insert, and lastline (also called “ex” mode).  You can navigate around a file and make changes to existing text in the "command" mode of vi. In command mode you can join lines together, delete characters, words or lines and replace single characters, strings and words. You cannot save the file to disk in this mode, nor can you insert new text. The "insert" mode of vi allows you to type new text into the file. Finally "lastline" mode provides a prompt at which you may save your file under its current name or under a new name, quit the vi editor, and set various options of vi. This mode also allows sophisticated editing to be done by advanced users.

 

Minimal editing

 

You can edit a file opened in the vi editor with just eight keys plus the arrow keys. Enter the vi editor by typing “vi” and the name of a file at any command prompt.

 

1) Enter the vi editor:

 

vi filea

 

You enter the vi editor in command mode and your display will change so that you may lose text previously displayed in the window. A blinking cursor will appear on the first character of the text. That cursor indicates your position in the text. You can now move around and change text. The cursor will display your current position as you move around.

 

2) Move around inside the file using the arrow keys. Move to the first and last letters of the file using arrow keys.

 

3) Remove text: Use the arrow keys to place the cursor on any character.

Type the letter x. Note what happened to the letter on which the cursor sat and to the position of the cursor.

 

4) Insert text in front of the cursor: Use the arrow keys to place the cursor on any character. Type the letter i. You are now in insert mode. Now type the word “one.” Observe the placement of the word “one” in front of the character. The “i” command allows you to add a new line at the beginning of a file. Go to the first character in the file, type the letter i, then hit RTN.

 

5) Return to command mode: Hit the ESC key.

 

6) Insert text after the cursor: Use the arrow keys to place the cursor on any character. Type the letter a. You are now in insert mode. Type the word “two.” Observe the placement of the word “two,” after the character. This command allows you to add a line to the end of a file. Go to the last character in the file, type the letter a and hit RTN.

 

Return to command mode: Hit the ESC key.

 

7) Move into lastline  mode: Hit “:.” A command prompt (:) will appear.

 

8) Write the file to disk and exit the editor: At the colon prompt, type wq and hit RTN (return). If you hit RTN without typing the “q,” you will return to command mode.

 

Summary of minimal vi:

 

Enter and leave the three modes:

 

Enter command mode: vi filename RTN

Leave command mode: a, i, (to insert mode) : (to lastline mode)

Enter edit mode: a, i (from command mode)

Leave edit mode: ESC (to command mode)

Enter lastline mode: : (from command mode)

Leave lastline mode: q RTN

 

Remove text: x (in command mode)

Add text: i, a (in insert mode)

 

Write a file to disk and quit: wq RTN (in lastline mode)

 
RESOURCES

vi cheat sheets to download:

http://www.trismegistus.net/vi_cheatsheets.htm (very complete)

http://www.kcomputing.com/kcvi.pdf  (minimal)

 

O'Reilly publications:

Learning the vi Editor, 6th Edition

vi Editor Pocket Reference

 

Hosted by www.Geocities.ws

1