vimtricks.wiki Concise Vim tricks, one at a time.

How do I paste the contents of a register at a specific line number without moving my cursor?

Answer

:{line}put {register}

Explanation

How it works

The :put Ex command pastes the contents of a register after a specified line. When combined with a line number, you can insert text at any position in the file without moving your cursor there first.

The syntax is:

:{line}put {register}
  • {line} is the line number after which the text will be placed.
  • {register} is the register to paste from (e.g., a, ", +, 0).

If no register is specified, the unnamed register " is used. If no line number is specified, the text is placed after the current line.

You can also paste above a line using :put! (with the exclamation mark), which places the text before the specified line instead of after.

Example

Suppose you have yanked a function signature into register a and you want to paste it after line 25:

:25put a

This inserts the contents of register a below line 25, no matter where your cursor currently is.

Paste the contents of the system clipboard after line 1:

:1put +

Paste the last yanked text at the very top of the file:

:0put

Line 0 is a special address meaning before line 1, so :0put places text at the very beginning of the file.

Tips

  • Use :put =expression to paste the result of a Vimscript expression. For example, :10put =strftime('%Y-%m-%d') inserts the current date after line 10.
  • Combine with :global: :g/pattern/put a pastes register a after every line matching the pattern.
  • The :put command always pastes linewise, even if the register contains characterwise text. This makes it predictable for inserting complete lines.

Next

How do you yank a single word into a named register?