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 63. Interact with the System Clipboard

Tip 63. Interact with the System Clipboard

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

Interact with the System Clipboard

• 155

Locating the System Paste Command

Throughout this tip, we’ll refer to the system paste command, and you can

substitute the appropriate mapping for your system. On OS X, the system

paste command is triggered by the Cmd - v mapping. We can use this inside

the Terminal or in MacVim, and it inserts the contents of the system clipboard.

Things aren’t quite so tidy on Linux and Windows. The standard mapping for

the system paste command is normally Ctrl - v . In Normal mode, this mapping

enables Visual-Block mode (Tip 21, on page 39), and in Insert mode it enables

us to insert characters literally or by a numeric code (Tip 17, Insert Unusual

Characters by Character Code, on page 32).

Some terminal emulators on Linux provide a modified version of Ctrl - v for

pasting from the system clipboard. It might be Ctrl - Shift - v or perhaps

Ctrl - Alt - v , depending on the system. Don’t worry if you can’t figure out what

the system paste command is for your setup. An alternative using the "* register is presented at the end of this tip.

Using the System Paste Command in Insert Mode

If we switch to Insert mode and then use the system paste command, we get

this strange result:

[1,2,3,4,5,6,7,8,9,10].each do |n|

if n%5==0

puts "fizz"


puts n



Something is wrong with the indentation. When we use the system paste

command in Insert mode, Vim acts as though each character has been typed

by hand. When the ‘autoindent’ option is enabled, Vim preserves the same level

of indentation each time we create a new line. The leading whitespace at the

start of each line in the clipboard is added on top of the automatic indent,

and the result is that each line wanders further and further to the right.

GVim is able to discern when text is pasted from the clipboard and adjust its

behavior accordingly, but when Vim runs inside a terminal this information

is not available. The ‘paste’ option allows us to manually warn Vim that we’re

about to use the system paste command. When the ‘paste’ option is enabled,

Vim turns off all Insert mode mappings and abbreviations and resets a host

of options, including ‘autoindent’ (look up :h 'paste' for the full list). That allows

us to safely paste from the system clipboard with no surprises.

report erratum • discuss

Chapter 10. Copy and Paste

• 156

When we’re finished using the system paste command, we should disable the

‘paste’ option again. That means switching back to Normal mode and then

running the Ex command :set paste!. Don’t you think it would be handy if there

were a way of toggling this option without leaving Insert mode?

The way that Vim behaves when ‘paste’ is enabled means that the usual

methods for creating custom mappings won’t work in Insert mode. Instead,

we can assign a key to the ‘pastetoggle’ option (:h 'pastetoggle' ):

:set pastetoggle=

Try executing that command line: it sets up the to toggle the paste option

on and off. It should work both in Insert and Normal modes. If you find the

mapping useful, add that line (or a variation of it) to your vimrc.

Avoid Toggling ‘paste’ by Putting from the Plus Register

If you’re running a version of Vim with system clipboard integration, then

you can avoid fiddling with the ‘paste’ option entirely. The Normal mode "+p

command pastes the contents of the plus register, which mirrors the system

clipboard (see The System Clipboard ("+) and Selection ("*) Registers, on page

148, for more details). This command preserves the indentation of the text in

the clipboard so you can expect no surprises, regardless of how the ‘paste’ and

‘autocommand’ options are set.

report erratum • discuss



Vim offers more than one way to repeat changes. We’ve already learned about

the dot command, which is useful for repeating small changes. But when we

want to repeat anything more substantial, we should reach for Vim’s macros.

Using these, we can record any number of keystrokes into a register and then

play them back.

Macros are ideal for repeating changes over a set of similar lines, paragraphs,

or even files. We’ll discover that there are two ways of executing a macro

across a set of targets—playing it back in series or running it multiple times

in parallel—and we’ll learn when to use each one.

When recording a sequence of commands, there’s always the chance that

we’ll make a mistake. But we needn’t discard a bad take. We can easily append

commands to the end of an existing macro. For more extensive amendments,

we can even paste the macro into a document, edit the sequence of commands

in place, and then yank it back into a register.

Sometimes we need to insert consecutive numbers into our text. In Tip 66,

on page 163, we’ll learn how to do that with a method that uses rudimentary

Vim script in combination with the expression register.

Like the game of Othello, Vim’s macros take a minute to learn and a lifetime

to master. But everyone—from beginners to experts—can get a lot of value

from this feature that makes it easy to automate tasks. Let’s see how.

report erratum • discuss

Chapter 11. Macros

• 158

Tip 64

Record and Execute a Macro

Macros allow us to record a sequence of changes and then play them back.

This tip shows how.

Many repetitive tasks involve making multiple changes. If we want to automate

these, we can record a macro and then execute it.

Capture a Sequence of Commands by Recording a Macro

The q key functions both as the “record” button and the “stop” button. To

begin recording our keystrokes, we type q{register} , giving the address of the

register where we want to save the macro. We can tell that we’ve done it right

if the word “recording” appears in the status line. Every command that we

execute will be captured, right up until we press q again to stop recording.

Let’s see this in action:


Buffer Contents


foo = 1

bar = 'a'

foobar = foo + bar

A ;

foo = 1;

bar = 'a'

foobar = foo + bar


var foo = 1;

bar = 'a'

foobar = foo + bar


var foo = 1;

bar = 'a'

foobar = foo + bar

Pressing qa begins recording and saves our macro into register a. We then

perform two changes on the first line: appending a semicolon and prepending

the word var. Having completed both of those changes, we press q to stop

recording our macro (:h q ).

We can inspect the contents of register a by typing the following:

:reg a

--- Registers --"a

A;^[Ivar ^[

report erratum • discuss

Record and Execute a Macro

• 159

It doesn’t make for easy reading, but the same sequence of commands that

we recorded moments ago should be recognizable. The only surprise might

be that the symbol ^[ is used to stand for the Escape key. See Keyboard Codes

in Macros, on page 177, for an explanation.

Play Back a Sequence of Commands by Executing a Macro

The @{register} command executes the contents of the specified register (see

:h @ ). We can also use @@ , which repeats the macro that was invoked most


Here’s an example:


Buffer Contents


var foo = 1;

bar = 'a'

foobar = foo + bar


var foo = 1;

bar = 'a'

foobar = foo + bar


var foo = 1;

var bar = 'a';

foobar = foo + bar


var foo = 1;

var bar = 'a';

var foobar = foo + bar;

We’ve executed the macro that we just recorded, repeating the same two

changes for each of the subsequent lines. Note that we use @a on the first line

and then @@ to replay the same macro on the next line.

In this example, we played the macro back by running j@a (and subsequently

j@@ ). Superficially, this has some resemblance to the Dot Formula. It involves

one keystroke to move ( j ) and two to act ( @a ). Not bad, but there’s room for


We have a couple of techniques at our disposal for executing a macro multiple

times. The setup differs slightly for each technique, but more importantly,

they react differently on encountering an error. I’ll explain the differences by

way of a comparison with Christmas tree lights.

If you buy a cheap set of party lights, the chances are that they will be wired

in series. If one bulb blows, they all go out. If you buy a premium set, they’re

more likely to be wired in parallel. That means any bulb can go out, and the

rest will be unaffected.

report erratum • discuss

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

Tip 63. Interact with the System Clipboard

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