Vim Tips Wiki

Pasting registers

1,619pages on
this wiki
Tip 1303 Printable Monobook Previous Next

created 2006 · complexity basic · author Mark Woodward · version 5.7

It is possible to paste the contents of any register into the current buffer or command-line.

In normal and visual modesEdit

In general, "{register}p pastes the contents of the register "{register}. For instance, "ap pastes the contents of the register "a.

Just as "+p (or "*p) pastes the contents of the clipboard, other registers can be pasted as well.

Use ":p to paste the last command. For example, you might want to save a complex search and replace (:%s///g) that you have just used.

Use "/p to paste the last search.

In insert and command-line modesEdit

In insert and command-line modes, you can use <C-r> to paste the contents of a register. In particular, <C-r>" pastes from the default (") register (so same as p in normal mode). This works with special registers as well, so <C-r>+ pastes from the clipboard and <C-r>/ pastes the last search.

Note that because Vim faithfully translates the contents of the clipboard to a buffer in insert and command-line modes, using <C-r>+ is susceptible to attacks if the contents of the clipboard cannot be fully trusted. This is sometimes called "clipboard hijacking". For instance, if one copies text from a website that contains a literal escape character (i.e. the character with ASCII value 27, not a representation of the escape character such as <Esc>), Vim will faithfully switch to normal mode and begin executing normal mode commands. Some browsers hide special characters when displaying a page, so it is not immediately obvious even if one has copied malicious text. Moreover, using clever div-tag positioning, even browsers that display special characters are susceptible to such attacks (proof of concept). It is possible to use <C-o>"+p instead, which prevents attacks of this form.

One mapping to avoid such attacks is:

inoremap <C-r>+ <C-g>u<C-\><C-o>"+gP

This will override the default behavior of <C-r>+ so that the pasting behavior is like that in normal mode. It first uses <C-g>u to break the undo sequence so that the pasting can be undone. Then <C-\><C-o> temporarily switches to normal mode without moving the cursor. It then accesses the "+ register and uses gP instead of the usual P to paste so that the cursor is positioned after what is pasted (just like the default <C-r>+). Note that because the clipboard is pasted in "one move" instead of being translated character-by-character (the default behavior), formatting options such as textwidth are not obeyed (use something like gqip to fix the formatting; see :help gq).

With the above mapping, the default behavior can still be retrieved by waiting a moment after typing <C-r> (so that Vim gives up on trying to use the custom mapping).



This illustrates the hijacking point. Executing the following puts some text in the a register.

:let @a = "abc\<Esc>:echo 'gotcha'\<CR>"

After that, pressing i to enter insert mode, then typing Ctrl-R a inserts "abc" but then executes the echo command. The Vim procedure to insert the keys literally is to press Ctrl-R twice. That is, type Ctrl-R Ctrl-R a which will insert the escape and newline (carriage return) characters into the text. :help i_CTRL-R_CTRL-R JohnBeckett (talk) 08:08, May 22, 2016 (UTC)

Around Wikia's network

Random Wiki