Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

SEARCH

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Exercises and Answers for Chapter 22...

The vi Text Editor

Review Question #1:

How do you start vi when you want to:

• Edit a file named document
• Edit a brand new file
• Open a file named document in read-only mode

How do you start Vim in vi-compatibility mode?

Answer

Edit a file named document:

vi document

Edit a brand new file:

vi

Open a file named document in read-only mode:

vi -R document
view document

To start Vim in vi-compatibility mode:

vim -C

If the vi commands starts Vim on your system, you can also use:

vi -C

Review Question #2:

How do you quit vi if you have already saved your work?

How do you save your work and then quit?

How do you quit without saving?

Answer

To quit vi if you have already saved your work, use either:

:q
ZZ

To save your work and then quit:

:wq

To quit without saving:

:q!

Review Question #3:

As you work with vi, your data is kept in a storage area. What is this storage area called?

The vi editor operates in two principle modes: command mode and input mode. Describe each mode.

How do you change from command mode to insert mode?

How do you change from insert mode to command mode?

Answer

The storage area is called the editing buffer.

In command mode, whichever keys you type are interpreted as commands. In input mode, everything you type is inserted directly into the editing buffer.

To change from command mode to insert mode, use one of the insert commands (i, a, I, A, o, O) or one of the replacement commands (r, R, s, C, cc, S, c).

To change from insert mode to command mode, press the <Esc> key.

Review Question #4:

Specify the best command to move the cursor to each of the following destinations. Whenever possible, use alphabetic keys.

• One position left, down, up, right
• Beginning of current line
• End of current line
• Beginning of previous line
• Beginning of next line
• Forward one word
• Backward one word
• Forward to next sentence
• Backward to previous sentence
• Forward to next paragraph
• Backward to previous paragraph
• Top line of the screen
• Middle line of the screen
• Bottom line of the screen
• Beginning of editing buffer
• End of editing buffer
• Down one screenful
• Up one screenful
• Down a half screenful
• Up a half screenful

Answer

h one position left
j one position down
k one position up
l one position right
0 beginning of current line
$ end of current line
- beginning of previous line
<Return> beginning of next line
w forward one word
b backward one word
) forward to next sentence
( backward to previous sentence
} forward to next paragraph
{ backward to previous paragraph
H top line of the screen
M middle line of the screen
L bottom line of the screen
1G beginning of editing buffer
gg beginning of editing buffer (alternative)
G end of editing buffer
^F down one screenful
^B up one screenful
^D down a half screenful
^U up a half screenful

Review Question #5:

Within command mode, how do you:

• Undo the last command that modified the editing buffer.
• Restore the current line to what it was when you moved to it.
• Repeat the last command that modified the editing buffer.

Answer

u undo the last command
U restore the current line
. repeat the last command

Applying Your Knowledge #1:

Start vi and create a brand new empty file named temp. Insert the following lines into the file:

one 1
two 2
three 3
four 4
five 5

Use a single vi command to save your work and quit.

Answer

1. vi temp
2. i
3. Type the five lines.
4. Press <Esc> to change to command mode.
5. ZZ (save your work and quit)

Applying Your Knowledge #2:

Start vi to edit the file temp from the previous question.

Using vi commands only:
• Move lines 2 through 4 to be after line 5.
• Undo the move.

Using vi commands only:
• Copy lines 2 through 4 to the top of the editing buffer.
• Undo the change.

At this point, the editing buffer should look like it did when you started.
• Quit without saving

Answer

Start vi to edit the file temp from Exercise #1:

vi temp

Using vi commands only...

Move lines 2 through 4 to be after line 5:

j   (move down to line 2)
3dd (delete 3 lines)
p   (paste the three lines below the current line)

Undo the move:

u   (undo the last change)
P   (paste the three lines above the current line)

Using vi commands, copy lines 2 through 4 to the top of the file:

3yy (yank 3 lines)
k   (move up to top of file)
P   (paste the three lines above the top line)

Quit without saving:

:q!

Applying Your Knowledge #3:

Start vi to edit the file temp from the previous question.

Using ex commands where possible:
• Move lines 2 through 4 to be after line 5.
• Undo the change.

Using ex commands where possible:
• Copy lines 2 through 4 to the top of the editing buffer
• Undo the change
• At this point, the editing buffer should look like it did when you started
• Quit without saving

Compare the vi commands you used in Exercise #2 with the ex commands you used in Exercise #3. What advantages did the ex commands have?

Answer

Start vi to edit the file temp from Exercise #1.

vi temp

Using ex commands where possible...

Copy lines 2 through 4 to the top of the file:

:2,4co0

Undo the copy:

u (undo the last change)

Quit without saving:

:q!

Compared to the vi commands, the ex commands can move and copy lines in a single operation. This makes it easier to undo when necessary.

Applying Your Knowledge #4:

Start vi to edit the file temp from Exercise #1:

• Insert the date and time at the bottom of the editing buffer

Where is the cursor? Why?

Without first moving the cursor:

• Use a single command to sort all lines in the editing buffer in reverse alphabetical order

• Quit without saving

Answer

Start vi to edit the file temp from Exercise #1

vi temp

Insert the date and time at the bottom of the editing buffer:

:$r !date

The cursor is on the bottom line, as that was the last line changed.

Without first moving the cursor, use a single command to sort all lines in the editing buffer in reverse alphabetical order:

!1Gsort -r

Quit without saving:

:q!

For Further Thought #1:

Once you are comfortable with the vi editor, you will find it to be quick, powerful, and easy to use. However, vi is a very complex program that takes a lot of effort to master. The backward compatible replacement, Vim, is even more powerful, more complex, and even harder to learn. Considering that vi is well over 30 years old and is so difficult to learn, why do you think it is still so popular in the Unix community?

Do you see a future in which complex tasks will be carried out exclusively by easy-to-use tools, or will there always be a need for programs like vi?

Answer

The vi text editor is popular because it works so well. It works well because it was designed by someone (Bill Joy) who really had a feeling for what he was doing. For many years, vi has been a standard tool that can be used with any Unix and Linux system, so it worth learning.

Easy-to-use tools will never be enough because many problems have inherently complex solution or, indeed, no solutions at all. Moreover, smart people like taking the time to master complex tools. They enjoy using their minds to the fullest extent possible, and they don't want to be held back by tools that were designed for less bright, less talented, and less industrious people.

As I observed in the exercises for Chapter 19, there is no program in the entire Unix toolbox that can't be mastered in less time than it takes to learn how to play the piano well.

For Further Thought #2:

Broadly speaking, vi has two different types of commands: screen-oriented vi commands and line-oriented ex commands. The two types of commands are completely different from one another and, indeed, were developed for different types of hardware. Nevertheless, they combine nicely to create a powerful editing environment. Why is this?

What does this tell you about the types of tools we should be designing for smart people?

Answer

The two types of commands represent two different ways of solving the problems that arise as we create moment-to-moment strategies to edit various types of text.

Broadly speaking, we can think of text in two different ways: as being composed of meaningful components, such as letters, words, sentences and paragraphs (the vi commands), or as being composed of structural components, such as blocks of lines (the ex commands). This is why the two types of commands complement one another so nicely.

Smart people should be given a variety of well-designed tools that can be customized as needed. Their tools should help them solve problems in a way that makes sense to them at each particular moment. When it comes to smart, creative, talented people, one size will never fit all.

Jump to top of page