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 92. Repeat the Previous Substitute Command

Tip 92. Repeat the Previous Substitute Command

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

Repeat the Previous Substitute Command



• 225



This longhand command spells out the following instruction: repeat the last

substitute command using the same flags, the same replacement string, and

the current search pattern, but use the % range. In other words, repeat the

last substitution across the entire file.

The next time you catch yourself tweaking the search history to prepend a %

to a substitute command that is otherwise all right, try hitting g& instead.



Amend the Range of a Substitute Command

Start with this code:

substitution/mixin.js

mixin = {

applyName: function(config) {

return Factory(config, this.getName());

},

}



Suppose that we want to extend it to look like this:

mixin = {

applyName: function(config) {

return Factory(config, this.getName());

},

applyNumber: function(config) {

return Factory(config, this.getNumber());

},

}



The new applyNumber function is almost identical to the existing one. So let’s

start off by duplicating the applyName function. Then we can use the substitute

command to change some occurrences of “Name” to “Number.” But there’s a

mistake in the example workflow shown in Table 25, Mistake in the Use of the

Substitute Command, on page 226.

Can you see what went wrong? We used the % symbol as a range, so it changed

every occurrence of “Name” to “Number.” Instead, we should have specified

a range to focus the substitute command on the lines of the second function

(the duplicate).

Not to worry. We can easily undo the mistake and fix it (see Table 26,

Changing the Range of the Substitute Command, on page 227).

The gv command enables Visual mode and rehighlights the last selection (we

discussed it in Tip 21, on page 39). When we press : in Visual mode, the

command line is prepopulated with the range :'<,'>, which focuses the next

Ex command on the selected lines.



www.it-ebooks.info



report erratum • discuss



Chapter 14. Substitution



Keystrokes



Buffer Contents



{start}



mixin = {

applyName: function(config) {

return Factory(config, this.getName());

},

}



Vjj



mixin = {

applyName: function(config) {

return Factory(config, this.getName());

},

}



yP



mixin = {

applyName:

return

},

applyName:

return

},

}



:%s/Name/Number/g



• 226



function(config) {

Factory(config, this.getName());

function(config) {

Factory(config, this.getName());



mixin = {

applyNumber: function(config) {

return Factory(config, this.getNumber());

},

applyNumber: function(config) {

return Factory(config, this.getNumber());

},

}



Table 25—Mistake in the Use of the Substitute Command

The :&& command requires some explanation, since the first and second &

symbols have different meanings. The first one forms the :& Ex command,

which repeats the last :substitute command (see :h :& ), while the second one

indicates that the flags from the previous :s command should be reused.



Discussion

We can always specify a new range and replay the substitution using the :&&

command. It doesn’t matter what range was used the last time. :&& by itself

acts on the current line, :'<,'>&& acts on the visual selection, and :%&& acts

on the entire file. As we saw already, the g& command is a handy shortcut

for :%&&.



www.it-ebooks.info



report erratum • discuss



Repeat the Previous Substitute Command



Keystrokes



Buffer Contents



u



mixin = {

applyName:

return

},

applyName:

return

},

}



gv



mixin = {

applyName:

return

},

applyName:

return

},

}



:'<,'>&&



• 227



function(config) {

Factory(config, this.getName());

function(config) {

Factory(config, this.getName());



function(config) {

Factory(config, this.getName());

function(config) {

Factory(config, this.getName());



mixin = {

applyName: function(config) {

return Factory(config, this.getName());

},

applyNumber: function(config) {

return Factory(config, this.getNumber());

},

}



Table 26—Changing the Range of the Substitute Command



Fixing the & Command

The & command is a synonym for :s, which repeats the last substitution. Unfortunately,

if any flags were used, the & command disregards them, meaning that the outcome

could be quite different from the previous substitution.

Making & trigger the :&& command is more useful. It preserves flags and therefore

produces more consistent results. These mappings fix the & command in Normal

mode and create a Visual mode equivalent:

nnoremap & :&&

xnoremap & :&&



www.it-ebooks.info



report erratum • discuss



Chapter 14. Substitution



• 228



Tip 93



Rearrange CSV Fields Using Submatches

In this tip, we’ll see how submatches captured from the search pattern can be

referenced in the replacement field.

Let’s say that we have a CSV file containing a list of email addresses along

with first and last names:

substitution/subscribers.csv

last name,first name,email

neil,drew,drew@vimcasts.org

doe,john,john@example.com



Now suppose that we want to swap the order of the fields so that the email

comes first, then the first name, and finally the last name. We could use this

substitute command to do it:









/\v^([^,]*),([^,]*),([^,]*)$

:%s//\3,\2,\1



In the pattern, [^,] matches anything that isn’t a comma. So ([^,]*) matches

zero or more consecutive non-commas and captures the result as a submatch

(refer to Tip 75, on page 189). We repeat this three times to capture each of

the three fields in the CSV file.

We can reference these submatches using the \n notation. So, in the replacement field, \1 will refer to the last name field, \2 refers to the first name field,

and \3 refers to the email field. Having sliced each line into the individual

fields, we can rearrange them into the order we want, namely \3,\2\,1—email,

first name, last name.

The result of running this command looks like this:

email,first name,last name

drew@vimcasts.org,drew,neil

john@example.com,john,doe



www.it-ebooks.info



report erratum • discuss



Perform Arithmetic on the Replacement



• 229



Tip 94



Perform Arithmetic on the Replacement

The replacement field needn’t be a simple string. We can evaluate a Vim script

expression and then use the result as the replacement string. Thus with a single

command, we can promote every HTML header tag in a document.

Suppose that we have an HTML document like this:

substitution/headings.html

Heading number 1



Number 2 heading



Another heading





We want to promote each heading, turning

into

,

into

,

and so on. To put it another way, we want to subtract one from the numeral

part of all HTML header tags.

We’ll harness the substitute command to do it. Here’s the general idea: we

write a pattern that matches the numeral portion of HTML header tags. Then

we write a substitute command that uses a Vim script expression to subtract

one from the number that was captured. When we run the substitute command globally across the entire file, all HTML header tags will be changed

with that single command.



The Search Pattern

The only thing that we want to change is the numeral part of the header tags,

so ideally we want to create a pattern that matches that and nothing else.

We don’t want to match all digits. We only want to match the ones that

immediately follow






/\v\<\/?h\zs\d



The \zs item allows us to zoom in on part of the match. To simplify our

example, we could say that a pattern of h\zs\d would match the letter “h” followed by any digit (“h1,” “h2,” and so on). The placement of \zs indicates that

the “h” itself would be excluded from the match, even though it is an integral

part of the broader pattern (we met the \zs item in Tip 77, on page 192, where

we compared it to Perl’s positive lookbehind assertion). Our pattern looks

slightly more complex, because we don’t just match “h1” and “h2,” but “



www.it-ebooks.info



report erratum • discuss



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

Tip 92. Repeat the Previous Substitute Command

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

×