Search code examples
vimyank

How to yank the current line and the line above it in Vim?


What I need is to yank the current line and the line just above it.

For instance, in the following example:

3   My test line
4   Line above current line
5   My current line |(cursor)
6   Line below current line

How do I yank lines 5 and 4 when my cursor is located on line 5?


Solution

  • yk should do it, as in Yank in the direction of up one line, since y will accept the next keystroke as a motion, and k alone represents motion up one line.

    If you need your cursor to return to its original position, just add a j as ykj. You will probably see the cursor move inelegantly on screen, but it gets the job done.