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 62. Paste from a Register

Tip 62. Paste from a Register

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

Chapter 10. Copy and Paste



• 152



cursor position can differ, depending on the contents of the register that is

being inserted.

In the case of xp , the register contains a single character. The p command

puts the contents of the register directly after the character that the cursor is

positioned on.

In the case of ddp , the register contains one complete line. The p command

puts the contents of the register on the line below the one that the cursor is

positioned on.

How can we know whether the p command will put the text from the register

after the current character or after the current line? It depends on how the

specified register was set. A line-wise yank or delete operation (such as dd ,

yy , or dap ) creates a line-wise register, whereas a character-wise yank or delete

(such as x , diw , or das ) creates a character-wise register. In general, the outcome of using the p command is fairly intuitive (see :h linewise-register for more

details).



Pasting Character-wise Regions

Suppose that our default register contains the text collection, and that we want

to paste as the first argument to a method call. Whether we use the p or P

command depends on where the cursor is positioned. Take this buffer:

collection = getCollection();

process(, target);



Compare it with this:

collection = getCollection();

process(, target);



In the first case we would use p , whereas in the second case we would use P .

I don’t find this to be very intuitive. In fact, I get it wrong often enough that

puP and Pup are practically muscle memory for me!

I don’t like having to think about whether a character-wise region of text

needs to go in front of the cursor or after it. For that reason, I sometimes

prefer to paste character-wise regions of text from Insert mode using the

{register} mapping rather than using the Normal mode p and P commands. Using this technique, the text from the register is always inserted in

front of the cursor position, just as though we were typing it in Insert mode.

From Insert mode, we can insert the contents of the unnamed register by

pressing " , or we can insert the contents of the yank register by pressing



www.it-ebooks.info



report erratum • discuss



Paste from a Register



• 153



0 (see Tip 15, on page 29, for more details). We can use this technique



to solve our problem from Oops! I Clobbered My Yank, on page 143:

Keystrokes



Buffer Contents



yiw



collection = getCollection();

process(somethingInTheWay, target);



jww



collection = getCollection();

process(somethingInTheWay, target);



ciw0



collection = getCollection();

process(collection, target);



Using the ciw command gives us an added bonus: the dot command now

replaces the current word with “collection.”



Pasting Line-Wise Regions

When pasting from a line-wise register, the p and P commands put the text

below or above the current line. This is more intuitive than the character-wise

behavior.

It’s worth noting that Vim also provides gp and gP commands. These also put

the text before or after the current line, but they leave the cursor positioned

at the end of the pasted text instead of at the beginning. The gP command is

especially useful when duplicating a range of lines, as demonstrated here:

Keystrokes



Buffer Contents



yap













KeystrokesBuffer Contents




gP





















KeystrokesBuffer Contents
KeystrokesBuffer Contents




We can use the duplicated text as a template, changing the contents of the

table cells to make it look how we want. Both the P and gP commands would



www.it-ebooks.info



report erratum • discuss



Chapter 10. Copy and Paste



• 154



have worked fine, except that the first one would leave our cursor positioned

above the inserted text. The gP command leaves our cursor positioned on the

second duplicate, which sets us up conveniently so that we can change it to

suit our needs.



Discussion

The p and P commands are great for pasting multiline regions of text. But

for short sections of character-wise text, the {register} mapping can be

more intuitive.



Tip 63



Interact with the System Clipboard

Besides Vim’s built-in put commands, we can sometimes use the system paste

command. However, using this can occasionally produce unexpected results

when running Vim inside a terminal. We can avoid these issues by enabling

the ‘paste’ option before using the system paste command.



Preparation

This tip is only applicable when running Vim inside the terminal, so you can

safely skip it if you always use GVim. We’ll start by launching Vim in the

terminal:







$ vim -u NONE -N



Enabling the ‘autoindent’ setting is a sure way to induce strange effects when

pasting from the system clipboard:







:set autoindent



Finally, we’ll need to copy the following code into the system clipboard.

Copying code listings from a PDF can produce strange results, so I recommend

downloading the sample code and then opening it in another text editor (or

a web browser) and using the system copy command:

copy_and_paste/fizz.rb

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

if n%5==0

puts "fizz"

else

puts n

end

end



www.it-ebooks.info



report erratum • discuss



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"

else

puts n

end

end



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.



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 62. Paste from a Register

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

×