Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [354]

By Root 1544 0
you could, for example, search for the next uppercase letter, using the command

/[A-Z]

Therefore, if the pattern you're searching for is not a static string, regular expressions can be used to specify just what you want.

You can couple navigation commands with other commands, such as deletion. For example, the command d$ will delete everything from the cursor to the end of the line; dG will delete everything from the cursor to the end of the file.

Saving Files and Quitting vi

Most of the commands dealing with files within vi are invoked from ex mode. You enter ex mode when you press the : key from command mode. This places the cursor on the last line of the display, allowing you to enter various extended commands.

For example, to write the file being edited, use the command :w. Typing : causes you to enter ex mode, and typing w followed by the Enter key completes the command. The command :wq writes the file and exits vi. (The command ZZ--from command mode, without the ":"--is similar to :wq, but checks first whether the file has been changed, and writes it only in this case.)

To quit vi without saving changes to the file, use the command :q!. Using :q alone will quit vi, but only if modifications to the file have been saved. The ! in :q! means to quit vi--and that you really mean it.

Editing Another File

To edit another file, use the :e command. For example, to stop editing test, and edit the file foo instead, use the command shown at the bottom of Figure 19-12.

Figure 19-12. Editing antoher file with vi

If you use :e without writing the file first, you'll get the following error message:

No write since last change (:edit! overrides)

At this point, you can use :w to save the original file, and then use :e, or you can use the command :e! foo, which tells vi to edit the new file without saving changes to the original. This can be useful if you edit a file and realize that you have screwed up. You can then use the :e! command; if you don't specify a filename, vi discards the changes and re-edits the current file.

Including Other Files

If you use the :r command, you can include the contents of another file in the vi buffer. For example, the command

:r foo.txt

inserts the contents of the file foo.txt after the current line.

Running Shell Commands

The :! command allows you to enter the name of a shell command, which is executed within vi. For example, the command

:!ls -F

executes the ls command and displays the results on your screen.

The :r! command is similar to :!, but includes the standard output of the command in the buffer. The command:

:r!ls -F

produces the screen shown in Figure 19-13.

Figure 19-13. Inserting results of a command in vi

If you need to execute a series of shell commands , it's often easier to use the suspend key (usually Ctrl-Z), provided you're using a shell that supports job control, such as zsh or bash.

Global Searching and Replacing

There are many more features of vi than are documented here; most of these features are implemented through combinations of the simple features we've seen. Here are one or two other tidbits most vi users find useful.

The command

:[x,y]s/pattern/replacement/flags

searches for pattern between lines x and y in the buffer, and replaces instances of pattern with the replacement text . pattern is a regular expression; replacement is literal text but can contain several special characters to refer to elements in the original pattern. The following command replaces the first occurrence of weeble with wobble on lines 1 through 10, inclusive:

:1,10s/weeble/wobble

Instead of giving line-number specification, you can use the % symbol to refer to the entire file. Other special symbols can be used in place of x and y. $ refers to the last line of the file. Leave x or y blank to refer to the current line.

Among the flags you can use are g to replace all instances of pattern on each line, and c to ask for confirmation for each replacement. In most instances, you will want to use the g flag,

Return Main Page Previous Page Next Page

®Online Book Reader