Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 4.1 More Command Combinations Chapter 4
Beyond the Basics
Next: 4.3 Making Use of Buffers
 

4.2 Options When Starting vi

In this handbook, you have invoked the vi editor with the command:

$ vi file

There are other options to the vi command that can be helpful. You can open a file directly to a specific line number or pattern. You can also open a file in read-only mode. Another option recovers all changes to a file that you were editing when the system crashed.

4.2.1 Advancing to a Specific Place

When you begin editing an existing file, you can call the file in and then move to the first occurrence of a pattern or to a specific line number. You can also specify your first movement by search or by line number right on the command line:[1]

[1] According to the POSIX standard, vi should use -c command instead of +command as shown here. Typically, for backwards compatibility, both versions are accepted.

$ vi +n file

Opens file at line number n.

$ vi + file

Opens file at last line.

$ vi +/pattern file

Opens file at the first occurrence of pattern.

In the file practice, to open the file and advance directly to the line containing the word Screen, enter:

KeystrokesResults
vi +/Screen practiceGraphic

Give the vi command with the option +/pattern to go directly to the line containing Screen.

As you see in the example above, your search pattern will not necessarily be positioned at the top of the screen. If you include spaces in the pattern, you must enclose the whole pattern within single or double quotes:[2]

[2] It is the shell that imposes the quoting requirement, not vi.

+/"you make"

or escape the space with a backslash:

+/you\ make

In addition, if you want to use the general pattern-matching syntax described in Chapter 6, Global Replacement, you may need to protect one or more special characters from interpretation by the shell with either single quotes or backslashes.

Using +/pattern is helpful if you have to leave an editing session in the middle. You can mark your place by inserting a pattern such as ZZZ or HERE. Then when you return to the file, all you have to remember is /ZZZ or /HERE.

NOTE: Normally, when you're editing in vi, the wrapscan option is enabled. If you've customized your environment so that wrapscan is always disabled (see Section 3.3.1, "Repeating Searches " in Chapter 3), you might not be able to use +/pattern. If you try to open a file this way, vi opens the file at the last line and displays the message "Address search hit BOTTOM without matching pattern."

4.2.2 Read-only Mode

There will be times when you want to look at a file but want to protect that file from inadvertent keystrokes and changes. (You might want to call in a lengthy file to practice vi movements, or you might want to scroll through a command file or program). You can enter a file in read-only mode and use all the vi movement commands, but you won't be able to change the file.

To look at a file in read-only mode, enter either:

$ vi -R file

or:

$ view file

(The view command, like the vi command, can use any of the command-line options for advancing to a specific place in the file.)[3] If you do decide to make some edits to the file, you can override read-only mode by adding an exclamation point to the write command:

[3] Typically view is just a link to vi.

:w!

or:

:wq!

If you have a problem writing out the file, see the problem checklists summarized in Appendix D, Problem Checklists.

4.2.3 Recovering a Buffer

Occasionally there is a system failure while you are editing a file. Ordinarily, any edits made after your last write (save) are lost. However, there is an option, -r, which lets you recover the edited buffer at the time of a system crash.

When you first log on after the system is running again, you will receive a mail message stating that your buffer has been saved. In addition, if you type the command:

$ ex -r

or:

$ vi -r

you will get a list of any files that the system has saved.

Use the -r option with a file name to recover the edited buffer. For example, to recover the edited buffer of the file practice after a system crash, enter:

$ vi -r practice

It is wise to recover the file immediately, lest you inadvertently make edits to the file, and then have to resolve a version skew between the preserved buffer and the newly edited file.

You can force the system to preserve your buffer even when there is not a crash by using the command :pre. You may find it useful if you have made edits to a file, then discover that you can't save your edits because you don't have write permission. (You could also just write out a copy of the file under another name or into a directory where you do have write permission. See Section 1.2.1, "Problems Saving Files " in Chapter 1.)

NOTE: Recovery for the various clones may work differently, and can change from version to version. It is best to check your local documentation. vile does not support any kind of recovery. The vile documentation recommends the use of the autowrite and autosave options. How to do this is described in Section 7.1, "Customizing vi " in Chapter 7.


Previous: 4.1 More Command Combinations Learning the vi EditorNext: 4.3 Making Use of Buffers
4.1 More Command Combinations Book Index4.3 Making Use of Buffers

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System