Visual Interface (VI): UNIX Editor

Introduction

vi is one of several popular editors available to UNIX programmers. Unlike many other popular text file editors, vi does not have a menu bar across the top of its interface frame and this can make it difficult for new users to create and edit files. This tutorial will introduce you to vi and show you how to create, edit and save files with this powerful and popular UNIX file editor. Although, there is a modest learning curve for new users, almost all experienced vi users, including myself, maintain that it is worth the effort.

This tutorial will not teach you every vi command that you will ever need to know. Rather, the goal of this tutorial is to introduce you to some of the commands that are available when editing files using vi. Also, I hope to inspire you to investigate vi further on your own for other commands that may be just as important to your particular writing and editing style.

This tutorial uses the notation [i] to indicate the "i" key on the keyboard. Do not type the "[" and "]" characters, just type the key that is indicated. If the character indicated is in upper case like in [I], you must hold the [Shift] key down while the character is pressed. Quotes are used to indicate that an entire line of text should be entered as shown.

I assume that you know how to login, logout and open xterm windows in UNIX.

Objectives

Topics

  1. Create your own quick reference sheet for vi. (5 minutes)
  2. Switch between vi editor modes. (5 minutes)
  3. Improve your (edit) vi cheat sheet. (10+ minutes)

Overview

When you create or open a file with vi, you are in the command mode by default, where you can delete characters, words, lines, etc. and perform other types of edits as well. However, to add information, you will have to change to one of several input modes. Also, you will learn the purpose and how to use vi's line editing mode.

In this tutorial you will learn the purpose of each mode and how to use it. After completing this tutorial, you will have your own quick reference sheet that you can print and add to as you learn more commands.

Create your own vi quick reference sheet

Creating your own quick reference sheet of vi commands will give you practice using vi to create and edit text files as well as providing a document for you to refer to if you forget a particular command that you remember learning.

Practice Exercise #1

  1. Open an xterm window if necessary.
  2. At the command-line prompt, type vi vi_reference.txt and press Enter. You are now in command mode and any keys that you type will be interpreted as commands instead of text for the file.
  3. Type i to enter insert mode. At this point, any keys that you type will be interpreted as text to be added to the file.
  4. Here's what insert mode looks like:

  5. Type the following into your file. If you make a mistake, you may backspace and retype the line correctly or just continue. If necessary, you will be able to correct any mistakes later.
    
           vi Quick Reference Sheet
           ------------------------
    
           i          - type i to insert text before character under cursor
    
           Esc        - press the Esc key to return to command mode
    
           ZZ         - save changes and exit vi
    
    

    Your file should now look like this:

    Notice the INSERT MODE indication in the lower right hand corner.

  6. Press the [Esc] key to exit insert mode and return to command mode.
  7. Your file should now look like this:

  8. Type ZZ to save your work and exit vi. You are now back at your shell prompt and your work has been saved in the file vi_reference.txt

"vi" editor modes

Although, there are several modes in vi, you really only have to learn the difference between command mode, input mode and the line editing mode. Here is a brief description of each mode:

Command Mode
This mode is used for many different types of editing operations, such as moving the cursor to different parts of your file and copying and pasting text in your document.

Operations like cut, copy and paste must be performed while in command mode and they may be performed on characters, words, sentences and larger blocks of text. Most commands are specified with only one or two typed characters. Note: The command characters must be typed in the correct case.

Each of the other modes is accessed by typing keys that change to that mode. For example, in practice exercise #1, you typed [i] to enter Insert mode, which allowed you to insert (add) new text to your file.

Command mode is also the mode that vi is in when it is first started. There are many ways to manipulate text in vi and each type of edit has several forms that are executed within command mode.

Input Modes
There are several different input modes that allow you to enter new text into your document. A description of each follows.
Line Editing

You can even execute other UNIX commands like ls from within vi.

Practice Exercise #2

  1. Open an xterm window if necessary.
  2. At the command-line prompt, type vi vi_reference.txt and press Enter to open the file you created in topic #1.
  3. Use the arrow keys to move your cursor to the middle of the description of the i command.
  4. Type I to enter insert mode. Where is the cursor located?
    Answer: At the start of the current line. Press the Esc key to return to command mode.
  5. Type A to enter append mode. Where is the cursor located?
    Answer: At the end of current line. Press the Esc key to return to command mode.
  6. Type O to insert a new line before the current line in open mode. Where is the cursor located?
    Answer: At start of a new blank line before the current line. Press the Esc key to return to command mode.
  7. Type i to enter insert mode. Where is the cursor located?
    Answer: Just before the current character. Press the Esc key to return to command mode.
  8. Type a to enter append mode. Where is the cursor located?
    Answer: Just after the current character. Press the Esc key to return to command mode.
  9. Type o to insert a new line after the current line in open mode. Where is the cursor located?
    Answer: At start of a new line after the current line. Press the Esc key to return to command mode.

Modify (edit) your vi quick reference sheet

In this topic, you will modify your reference sheet and add each of the commands shown in this topic. These commands were used for entering new text into file. Each command first moved the cursor then started a new mode, which allows the user to enter new text at the point of the cursor.

There are many commands in vi that can be used to reduce the time it takes to edit to your files. You can move the cursor line by line and character by character using the arrow keys, but there are better ways if you need to move far away from the current position of the cursor. You've already seen that [I], [A], and [O] each move the cursor farther than arrow keys before beginning a version of inserting.

There are commands that move the cursor by words, lines, sentences, paragraphs and more. The cursor can be moved forward in the file or backward. Each of these commands can be preceded by a number to indicate how many time to execute the command. For example, the command [4f] moves the cursor forward four words. Rather than provide you with an exhaustive list of commands to learn, this tutorial will explain the use of the more common commands and show you how to find more information when you're ready to learn even more commands.

Practice Exercise #3

Follow these steps to modify your vi_reference.txt file. You should review the the commands explained in topic #2 and try the new ones suggested.

  1. Open an xterm window if necessary and open the "vi_reference.txt" file you created in topic #1.
  2. Turn the line number feature on. This will help make it easier to move directly to the line that you're interested in editing.
  3. a. Type the : (colon) key. This will open a whole set of additional vi commands.
    b. Type set number
    c. Press [Enter] to complete the command.
    Now, there are line numbers displayed to the left of each line in your file.
  4. Type 4G to move to line #4 of the file.
  5. Type o to open a new line after the insert command line.
  6. Add the following new reference lines to your file by typing:
    a - type a to insert text after the character under the cursor
    o - type o to open a new line after current line

    I - type I to insert text at start of current line
    A - type A to append text to end of current line
    O - type O to open a new line before current line

    If you've made any mistakes, just continue. You'll learn additional editing commands in the next exercise.
  7. Press [Esc] to return to command mode after you've finished typing the lines above.

Your reference sheet should look like the following perhaps with a few typing errors that you'll correct in the next exercise.

       vi Quick Reference Sheet
       ------------------------

       i        - type i to insert text before character under cursor
       a        - type a to insert text after character under cursor
       o        - type o to open a new line after current line

       I        - type I to insert text at start of current line
       A        - type A to append text to end of current line
       O        - type O to open a new line before current line

       [Esc]    - press the Esc key to return to command mode

       ZZ       - save changes and exit vi

Practice Exercise #4

In this exercise, you will correct any typing mistakes that you may have made during the previous exercises. Since, it is not possible for me to know exactly which typing errors you may have made, this exercise will be different from the earlier exercises. Instead of a step by step approach, I'll just provide a list of vi commands that can be used to edit characters in your file. Be sure to try each of them as you edit your "vi_reference.txt" file.

  1. Type /Esc key to move the cursor to the next instance of the phrase "Esc key".
  2. Type the number 0 to move the cursor to the start of the current line without starting adding new text.
  3. Type i and the following text "I - type I to insert text at start of current line".
  4. What command could have been used in place of the last two steps? ___________ ANsWER: I
  5. Add each of the following vi commands to your reference file

Add the text editing commands to your reference file

  1. Add each of the following text manipulation commands to your reference file.

Use the man pages for more information

In this topic you will view the on-line programmer's manual page for vi. These pages explain each of the options that can be used when starting vi.

Summary

In this lesson you have learned how to create, edit and save text files using vi, one of the more popular editors available to UNIX programmers.

As you work in vi and learn new commands, be sure to add them to your quick reference sheet so that you will be able refer to them in the future.

Vocabulary

Append mode

The mode entered when A is typed. Text is inserted at the end of the current line.

Command mode

The default mode. vi commands work only from this mode.

Editor

A program used to create program and data files.

Insert mode

The mode entered when i is typed. Text is inserted before the cursor. Use I to begin inserting text at the beginning of the current line.

Open mode

The mode entered when o is typed. Text is inserted on the line following the current line.

Set command

Used to change vi environment settings, such as the tab stops, auto indent, numbering, etc.

Where to Get More Information