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

How do I copy a character from the line directly above or below while staying in insert mode?

Answer

<C-y> and <C-e>

Explanation

In Insert mode, <C-y> copies the character at the same column position from the line above the cursor, and <C-e> copies the character from the line below. Each keypress inserts one character and advances the cursor, so tapping them repeatedly copies multiple characters column by column — without ever leaving Insert mode.

Note: these are entirely different from their Normal mode meanings (<C-y> scrolls up, <C-e> scrolls down).

How it works

  • <C-y> — (Insert mode) insert the character from the same column in the line above
  • <C-e> — (Insert mode) insert the character from the same column in the line below

If the adjacent line is shorter than the current column, the keypress does nothing.

Example

Given:

func createServer(host, port) {

}

With the cursor on the blank line between the braces, pressing <C-e> once copies }, since the line below is }. More usefully, position the cursor on a new line below func createServer(host, port) { and use <C-y> repeatedly to copy out func character by character when starting a sibling function.

Tips

  • Faster than <Esc>kyy jPkdd when you only need a portion of an adjacent line
  • Works mid-word: if you have typed part of a similar identifier, resume copying from the matching column above
  • Combine with <C-o> (execute one Normal command without leaving Insert mode) to reposition first, then copy with <C-y> or <C-e>

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?