Harley Hahn's Guide to
|
A Personal Note
Chapters...
Command
INSTRUCTOR |
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
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
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
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
Answer
Review Question #5: Within command mode, how do you:
• Undo the last command that modified the editing buffer.
Answer
Applying Your Knowledge #1: Start vi and create a brand new empty file named temp. Insert the following lines into the file:
one 1
Use a single vi command to save your work and quit. Answer
1. vi temp
Applying Your Knowledge #2: Start vi to edit the file temp from the previous question.
Using vi commands only:
Using vi commands only:
At this point, the editing buffer should look
like it did when you started.
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)
Undo the move:
u (undo the last change)
Using vi commands, copy lines 2 through 4 to the top of the file:
3yy (yank 3 lines)
Quit without saving: :q! Applying Your Knowledge #3: Start vi to edit the file temp from the previous question.
Using ex commands where possible:
Using ex commands where possible:
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. Exercises: Introduction | Chapter list
© All contents Copyright 2024, Harley Hahn
|