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 28. Execute a Command on One or More Consecutive Lines

Tip 28. Execute a Command on One or More Consecutive Lines

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

Execute a Command on One or More Consecutive Lines











• 55



:1

:print

1



This file contains only five lines. If we wanted to jump to the end of the file,

we could enter :5 or we could use the special $ symbol:











:$

:p

5



Here we’ve used :p, which is the abbreviated form of :print. Instead of splitting

up the two commands, we could roll them into a single incantation:









:3p

3 Practical Vim



That moves the cursor to line 3 and then echoes the contents of that line.

Remember, we’re just using the :p command for illustrative purposes here. If

we had issued the command :3d, then we would have jumped to line 3 and

deleted it in a single move. The equivalent Normal mode commands would

be 3G followed by dd . So this is one example where an Ex command can be

quicker than a Normal mode command.



Specify a Range of Lines by Address

So far, we’ve specified addresses as a single line number. But we can also

specify a range of lines. Here’s an example:









:2,5p

2

3

Practical Vim

4

Practical Vim



5



That prints each line from 2 to 5, inclusive. Note that after running this

command, the cursor would be left positioned on line 5. In general, we could

say that a range takes this form:

:{start},{end}



Note that both the {start} and {end} are addresses. So far, we’ve looked at using

line numbers for addresses, but we’ll soon see that using a pattern or a mark

is also possible.

We can use the . symbol as an address to represent the current line. So, we

can easily compose a range representing everything from here to the end of

the file:



www.it-ebooks.info



report erratum • discuss



Chapter 5. Command-Line Mode











• 56



:2

:.,$p

2

3

Practical Vim

4

Practical Vim



5



The % symbol also has a special meaning—it stands for all the lines in the

current file:









:%p

1

2

3

Practical Vim

4

Practical Vim



5



This is equivalent to running :1,$p. Using this shorthand in combination with

the :substitute command is very common:







:%s/Practical/Pragmatic/



This command tells Vim to replace the first occurrence of “Practical” with

“Pragmatic” on each line. We’ll learn more about this command in Chapter

14, Substitution, on page 215.



Specify a Range of Lines by Visual Selection

Instead of addressing a range of lines by number, we could just make a

visual selection. If we ran the command 2G followed by VG , we would make a

visual selection that looks like this:





Practical Vim

Practical Vim







If we press the : key now, the command-line prompt will be prepopulated

with the range :'<,'>. It looks cryptic, but you can think of it simply as a range

standing for the visual selection. Then we can specify our Ex command, and

it will execute on every selected line:









:'<,'>p

2

3

Practical Vim

4

Practical Vim



5



www.it-ebooks.info



report erratum • discuss



Execute a Command on One or More Consecutive Lines



• 57



This range can be really handy if we want to run a :substitute command on a

subset of the file.

The '< symbol is a mark standing for the first line of the visual selection, while

'> stands for the last line of the visual selection (see Tip 53, on page 126, for

more about marks). These marks persist even when we leave Visual mode. If

you try running :'<,'>p straight from Normal mode, it will always act on the

lines that most recently formed a Visual mode selection.



Specify a Range of Lines by Patterns

Vim also accepts a pattern as an address for an Ex command, such as the

one shown here:









://,/<\/html>/p

2

3

Practical Vim

4

Practical Vim



5



This looks quite complex, but it follows the usual form for a range: :{start},{end}.

The {start} address in this case is the pattern //, while the {end} address

is /<\/html>/. In other words, the range begins on the line containing an opening

tag and ends on the line containing the corresponding closing tag.

In this particular case, we could achieve the same result using the address

:2,5, which is shorter but more brittle. If we use patterns to specify the range,

then our command will always operate on the entire element,

no matter how many lines it comprises.



Modify an Address Using an Offset

Suppose that we wanted to run an Ex command on every line inside the

block but not on the lines that contain the and

tags themselves. We could do so using an offset:









://+1,/<\/html>/-1p

3 Practical Vim

4

Practical Vim





The general form for an offset goes like this:

:{address}+n



If n is omitted, it defaults to 1. The {address} could be a line number, a mark,

or a pattern.



www.it-ebooks.info



report erratum • discuss



Chapter 5. Command-Line Mode



• 58



Suppose that we wanted to execute a command on a particular number of

lines, starting with the current line. We could use an offset relative to the

current line:









:2

:.,.+3p



The . symbol stands for the current line, so :.,.+3 is equivalent to :2,5 in this

case.



Discussion

The syntax for defining a range is very flexible. We can mix and match line

numbers, marks, and patterns, and we can apply an offset to any of them.

This table summarizes a few of the symbols that can be used to create

addresses and ranges for Ex commands:

Symbol



Address



1



First line of the file



$



Last line of the file



0



Virtual line above first line of the file



.



Line where the cursor is placed



'm



Line containing mark m



'<



Start of visual selection



'>



End of visual selection



%



The entire file (shorthand for :1,$)



Line 0 doesn’t really exist, but it can be useful as an address in certain contexts. In particular, it can be used as the final argument in the :copy {address}

and :move {address} commands when we want to copy or move a range of lines

to the top of a file. We’ll see examples of these commands in the next two tips.

When we specify a [range], it always represents a set of contiguous lines. It’s

also possible to execute an Ex command on a set of noncontiguous lines using

the :global command. We’ll learn more about that in Chapter 15, Global Commands, on page 237.



www.it-ebooks.info



report erratum • discuss



Duplicate or Move Lines Using ‘:t’ and ‘:m’ Commands



• 59



Tip 29



Duplicate or Move Lines Using ‘:t’ and ‘:m’ Commands

The :copy command (and its shorthand :t) lets us duplicate one or more lines

from one part of the document to another, while the :move command lets us

place them somewhere else in the document.

For demonstration purposes, we’ll use this shopping list:

ex_mode/shopping-list.todo

Line 1 Shopping list

2

3

4

5

6



Hardware Store

Buy new hammer

Beauty Parlor

Buy nail polish remover

Buy nails



Duplicate Lines with the ‘:t’ Command

Our shopping list is incomplete: we also need to buy nails at the hardware

store. To fix the list, we’ll reuse the last line of the file, creating a copy of it

below “Hardware Store.” We can easily do so using the :copy Ex command:

Keystrokes



Buffer Contents



{start}



Shopping list

Hardware Store

Buy new hammer

Beauty Parlor

Buy nail polish remover

Buy nails



:6copy.



Shopping list

Hardware Store

Buy nails

Buy new hammer

Beauty Parlor

Buy nail polish remover

Buy nails



The format of the copy command goes like this (see :h :copy



):



:[range]copy {address}



In our example, the [range] was line 6. For our {address}, we used the . symbol,

which stands for the current line. So we can read the :6copy. command as

“Make a copy of line 6 and put it below the current line.”



www.it-ebooks.info



report erratum • discuss



Chapter 5. Command-Line Mode



• 60



We could shorten the :copy command to only two letters, as :co. Or we can be

even more succinct by using the :t command, which is a synonym for :copy.

As a mnemonic, you can think of it as copy TO. This table shows a few

examples of the :t command in action:

Command



Effect



:6t.



Copy line 6 to just below the current line



:t6



Copy the current line to just below line 6



:t.



Duplicate the current line (similar to Normal mode yyp )



:t$



Copy the current line to the end of the file



:'<,'>t0



Copy the visually selected lines to the start of the file



Note that :t. duplicates the current line. Alternatively, we could achieve the

same effect using Normal mode yank and put commands ( yyp ). The one notable

difference between these two techniques for duplicating the current line is

that yyp uses a register, whereas :t. doesn’t. I’ll sometimes use :t. to duplicate

a line when I don’t want to overwrite the current value in the default register.

In this example, we could have used a variant of yyp to duplicate the line we

wanted, but it would require some extra moving around. We would have to

jump to the line we wanted to copy ( 6G ), yank it ( yy ), snap back to where we

started ( ), and use the put command ( p ) to duplicate the line. When

duplicating a distant line, the :t command is usually more efficient.

In Ex Commands Strike Far and Wide, on page 54, we observed the general

rule that Normal commands act locally, whereas Ex commands are long range.

This example demonstrates this principle in action.



Move Lines with the ‘:m’ Command

The :move command looks similar to the :copy command (see :h :move



):



:[range]move {address}



We can shorten it to a single letter: :m. Suppose that we want to move the

Hardware Store section after the Beauty Parlor section. We could do so using

the :move command as shown in Table 10, Moving a Set of Lines with the ‘:m’

Command, on page 61.

Having made our visual selection, we simply have to run the command :'<,'>m$.

Alternatively, we could run dGp . This breaks down like this: d to delete the

visual selection, G to jump to the end of the file, and p to paste the text that

was deleted.



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 28. Execute a Command on One or More Consecutive Lines

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

×