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

How do I yank and paste a vertical block of text using blockwise registers?

Answer

<C-v>jj"ay then "ap

Explanation

How it works

Vim registers remember not just the text content but also the type of selection that was used to yank it: characterwise, linewise, or blockwise. When you yank text using visual block mode (Ctrl-V), the register stores it as a blockwise selection. When you paste it, the text is inserted as a vertical column rather than inline.

The sequence <C-v>jj"ay breaks down as:

  • <C-v> -- enter visual block mode
  • jj -- extend the selection down two lines (adjust as needed)
  • "ay -- yank the block selection into register a

When you later paste with "ap, the text is inserted as a column to the right of the cursor, pushing existing text on each line to the right. Using "aP inserts the column to the left of the cursor.

This is fundamentally different from linewise or characterwise paste. A blockwise paste affects multiple lines simultaneously, inserting text at the same column position on each line.

Example

Suppose you have a table and want to duplicate a column:

Alice   100
Bob     200
Carol   300
  1. Position cursor on 1 in 100
  2. Enter visual block mode: <C-v>
  3. Select the number column: jj$
  4. Yank into register a: "ay
  5. Move to the end of line 1: $
  6. Paste: "ap

Result:

Alice   100  100
Bob     200  200
Carol   300  300

You can also change the register type after the fact using setreg(). For example, call setreg('a', @a, 'b') converts register a to blockwise. This lets you paste characterwise text as a block column, which is a powerful transformation technique.

Next

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