Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 71. Edit the Contents of a Macro

Tip 71. Edit the Contents of a Macro

Tải bản đầy đủ - 0trang

Edit the Contents of a Macro

• 177

under the cursor (see :h ~ ). Instead of using ~ , let’s update the macro to

use the command vU , which uppercases the letter under the cursor (see

:h v_U ).

Keyboard Codes in Macros

In this example, we are working with a relatively simple register. But things can get

messy quickly if we attempt to edit a larger macro. For example, let’s inspect the

macro that was recorded in Tip 69, on page 169:

:reg a

--- Registers --"a

Omoul<80>kb<80>kbdule Rank^[j>GGoend^[

Notice anything strange? First of all, the ^[ symbol appears a couple of times. No

matter whether you press or , that’s how Vim represents the Escape key.

Stranger still is the <80>kb symbol, which represents the backspace key. Study the

keystrokes. When I recorded this macro, I started off by typing “moul.” Upon seeing

my mistake, I hit the backspace key a couple of times and then typed out “dule,” the

rest of the word.

This action is of no practical consequence. If I replay those keystrokes, Vim will

reproduce my mistake followed by my correction. The net result will be correct. But

it does make the register harder to read and more fiddly to edit.

Paste the Macro into a Document

The registers that we use for recording macros are the very same with which

the yank and put operations interact. So if we want to make changes to the

macro saved in register a, we simply have to paste it into the document, where

we can edit it as plain text.

Let’s press G and jump to the end of the current document. We want to paste

the contents of register a into a new line. The simplest way of doing that is

with the :put command:

:put a

Why didn’t we just use the "ap command? In this context, the p command

would paste the contents of the a register after the cursor position on the

current line. The :put command, on the other hand, always pastes below the

current line, whether the specified register contains a line-wise or a characterwise set of text.

report erratum • discuss

Chapter 11. Macros

• 178

Edit the Text

Now we can edit the macro as plain text. The sequence of commands shown

in Table 20, Editing the Macro as Plain Text, on page 178 replaces the ~ character with vU.


Buffer Contents





s vU


Table 20—Editing the Macro as Plain Text

Yank the Macro from the Document Back into a Register

We’ve got the sequence of commands looking just the way we want it to, so

we can yank it from the document back into a register. The simplest way is

to run "add (or :d a), but this could cause us problems later. The dd command

performs a line-wise deletion. The register contains a trailing ^J character:

:reg a


This character represents a newline, which in most circumstances won’t

matter. But sometimes this trailing newline could change the meaning of the

macro. As a precaution, using a character-wise yank to get the characters

from the document back into the register is a safer bet:


Buffer Contents


// last line of the file proper



// last line of the file proper



// last line of the file proper



// last line of the file proper

When we run the command 0 followed by "ay$ , we yank every character on

that line except for the carriage return. Having captured everything that we

want to keep into register a, we can then run dd to delete the line. This will

end up in the default register, but we won’t use it.

Having followed these steps, register a now contains a new and improved

macro. We can use it on the example text that we met at the start of this tip.

report erratum • discuss

Edit the Contents of a Macro

• 179


Being able to paste a macro into the document, edit it right there, and then

yank it back into a register and execute it is very handy. But the register can

be fussy to work with for the reasons noted in Keyboard Codes in Macros, on

page 177. If you only have to append a command at the end of your macro,

following the procedure outlined in Tip 68, on page 168, is simpler.

Since Vim’s registers are no more than containers for strings of text, we can

also manipulate them programmatically using Vim script. For example, we

could use the substitute() function (which is not the same as the :substitute command! See :h substitute() ) to perform the same edit as before:

:let @a=substitute(@a, '\~', 'vU', 'g')

If you’re curious about this approach, look up :h function-list

for more ideas.

report erratum • discuss

Part V


This part of the book is devoted to patterns, which

are integral to some of Vim’s most powerful commands. We’ll look at some tricks that make it easier

to compose regular expressions and to search for

text verbatim. We’ll study the mechanics of the

search command itself and then explore two powerful Ex commands: :substitute, which allows us to

find occurrences of one pattern and replace them

with something else, and :global, which lets us run

any Ex command on each line that matches a particular pattern.


Matching Patterns and Literals

In this part of the book, we’ll talk about search, substitute, and global commands. But first, we’ll focus on the core that drives each of them: Vim’s search

engine. Have you ever wondered how Vim’s regular expressions work or how

to turn them off?

Vim’s regular expression engine may be somewhat different from the one

you’re accustomed to using. We’ll see that the most confusing discrepancies

can be smoothed out with the very magic pattern switch. Certain characters

have a special meaning by default in Vim’s search field, which can complicate

matters when we just want to match something verbatim. We’ll learn how to

disable all of these special meanings at a stroke, with the very nomagic literal


We’ll focus on a couple of special items that can be used in Vim’s patterns:

zero-width delimiters that can mark the boundaries of a word or a search

match. We’ll finish with an in-depth discussion of how to deal with the

handful of characters that retain special meaning, even when we use the \V

literal switch.

Tip 72

Tune the Case Sensitivity of Search Patterns

We can tune the case sensitivity of Vim’s search globally or on a per-search


report erratum • discuss

Chapter 12. Matching Patterns and Literals

• 184

Setting Case Sensitivity Globally

We can make Vim’s search patterns case insensitive by enabling the ‘ignorecase’

setting. Be aware that this setting has a side effect that influences the

behavior of Vim’s keyword autocompletion, as discussed in Autocompletion

and Case Sensitivity, on page 276.

Setting Case Sensitivity per Search

We can override Vim’s default case sensitivity using the \c and \C items. Lowercase \c causes the search pattern to ignore case, while the uppercase \C item

forces case sensitivity. If either of these items is used in a search pattern, the

value of ‘ignorecase’ is overridden for that search.

Note that these items can be used anywhere in a pattern. If you realize that

you need a case sensitive search after you typed out the full pattern, just tack

\C on at the end, and it will affect everything that went before it.

Enabling Smarter Default Case Sensitivity

Vim provides an extra setting that makes an effort to predict our case sensitivity intentions. This is the ‘smartcase’ option. When enabled, ‘smartcase’ has

the effect of canceling out the ‘ignorecase’ setting any time that we include an

uppercase character in our search pattern. In other words, if our pattern is

all lowercase, then the search will be case insensitive. But as soon as we

include an uppercase letter, the search becomes case sensitive.

Does that sound complicated? Try it out and you’ll find that it feels quite

intuitive. And remember that we can always force case sensitivity or insensitivity for an individual search by including the \C or \c items. Table 21, Tuning

the Case Sensitivity of Search Patterns, on page 185, illustrates a matrix of

case sensitivity options. A similar table can be found in Vim’s built-in documentation by looking up :h /ignorecase .

Tip 73

Use the \v Pattern Switch for Regex Searches

Vim’s regular expression syntax is closer in style to POSIX than to Perl. For

programmers who already know Perl’s regexes, this can be a source of frustration. Using the very magic pattern switch, we can make Vim adopt a more familiar

syntax for regular expressions.

report erratum • discuss

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 71. Edit the Contents of a Macro

Tải bản đầy đủ ngay(0 tr)