Lamb Linda - Learning the vi and Vim Editors стр 16.

Шрифт
Фон

26 | Chapter 2:Simple Editing

transposition, place the cursor on v and press x, then p. By coincidence, the word

transpose helps you remember the sequence xp; x stands for trans , and p stands for pose .

There is no command to transpose words. The section More Examples of Mapping

Keys on page 107 discusses a short sequence of commands that transposes two words.

Copying Text

Often you can save editing time (and keystrokes) by copying a part of your file to

y use in other places. With the two commands y (for yank) and p (for put), you can

copy any amount of text and put that copied text in another place in the file. A yank

command copies the selected text into a special buffer, where it is held until another

yank (or deletion) occurs. You can then place this copy elsewhere in the file with the

put command.

As with change and delete, the yank command can be combined with any movement

command (yw, y$, 4yy). Yank is most frequently used with a line (or more) of text,

because to yank and put a word usually takes longer than simply to insert the word.

The shortcut yy operates on an entire line, just as dd and cc do. But the shortcut Y, for

some reason, does not operate the way D and C do. Instead of yanking from the current

position to the end of the line, Y yanks the whole line; that is, Y does the same thing as

yy.

Suppose you have in your file practice the text:

With a screen editor you can

scroll the page.

move the cursor.

delete lines.

You want to make three complete sentences, beginning each with With a screen editor

you can . Instead of moving through the file, making this edit over and over, you can

use a yank and put to copy the text to be added.

Keystrokes

Results

yy

With a screen editor you can

scroll the page.

move the cursor.

delete lines.

Yank the line of text that you want to copy into the buffer. The cursor can be anywhere on the

line you want to yank (or on the first line of a series of lines).

2j

With a screen editor you can

scroll the page.

move the cursor.

delete lines.

Move the cursor to where you want to put the yanked text.

P

With a screen editor you can

scroll the page.

Simple Edits | 27

Keystrokes

Results

With a screen editor you can

move the cursor.

delete lines.

Put the yanked text above the cursor line with P.

jp

With a screen editor you can

scroll the page.

With a screen editor you can

move the cursor.

With a screen editor you can

delete lines.

Move the cursor down a line and put the yanked text below the cursor line with p.

Yanking uses the same buffer as deleting. Each new deletion or yank replaces the pre-

vious contents of the yank buffer. As well see in Chapter 4, up to nine previous yanks

or deletions can be recalled with put commands. You can also yank or delete directly

into up to 26 named buffers, which allows you to juggle multiple text blocks at once.

Repeating or Undoing Your Last Command

Each edit command that you give is stored in a temporary buffer until you give the next

command. For example, if you insert the after a word in your file, the command used

to insert the text, along with the text that you entered, is temporarily saved.

Repeat

Any time you make the same editing command over and over, you can save time

. by duplicating it with the repeat command, the period (.). Position the cursor

where you want to repeat the editing command, and type a period.

Suppose you have the following lines in your file:

With a screen editor you can

scroll the page.

With a screen editor you can

move the cursor.

You can delete one line, and then, to delete another line, simply type a period.

Keystrokes

Results

dd

With a screen editor you can

scroll the page.

move the cursor.

Delete a line with the command dd.

.

With a screen editor you can

scroll the page.

Repeat the deletion.

28 | Chapter 2:Simple Editing

Older versions of vi have problems repeating commands. For example, such versions

may have difficulty repeating a long insertion when wrapmargin is set. If you have such

a version, this bug will probably bite you sooner or later. Theres not a lot you can do

about it after the fact, but it helps to be forewarned. (Modern versions do not seem to

have this problem.) There are two ways you can guard against a potential problem

when repeating long insertions. You can write your file (:w) before repeating the inser-

tion (returning to this copy if the insertion doesnt work correctly). You can also turn

off wrapmargin like this:

:set wm=0

In the later section More Examples of Mapping Keys on page 107, well show you

an easy way to use the wrapmargin solution. In some versions of vi, the command CTRL-

@ repeats the most recent insertion. CTRL-@ is typed in insert mode and returns you

to command mode.

Undo

As mentioned earlier, you can undo your last command if you make an error.

u Simply press u. The cursor need not be on the line where the original edit was

made.

To continue

the previous example, showing deletion of lines in the file practice:

Ваша оценка очень важна

0
Шрифт
Фон

Помогите Вашим друзьям узнать о библиотеке