Yanking an Entire Buffer

When editing a document one often needs to yank the entire buffer content. As with most operations, Neovim provides several different ways to accomplish this task.

For each of the examples below, we will start with the following buffer content and cursor position:

Initial Conditions
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
NORMAL
57%
4:1
 

Common Method

Perhaps the most common method to achieve this task is the following sequence:

  1. gg to move the cursor to the top of the buffer.

  2. v to enter visual mode.

  3. G to move the cursor to the bottom of the buffer.

  4. y to yank the selected content.

Common Method
ggVGy
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
NORMAL
Top
1:1
6 lines yanked

Let's check the unnamed register to confirm that the full buffer content was yanked:

Common Method
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
COMMAND
Top
1:1
:reg "
:reg "
Type Name Content
  l  ""   red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f

This method works well, and is probably so common because it is consistent with yanking smaller portions of the buffer. Besides requiring the most keystrokes, this method also has the disadvantage of moving the cursor to the top of the screen.

Shorter Method

A slightly shorter method leverages motions a bit more directly to save a keystroke:

  1. gg to move the cursor to the top of the buffer.

  2. y to yank the selected content.

  3. G to move the cursor to the bottom of the buffer.

Shorter Method
ggyG
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
NORMAL
Top
1:1
6 lines yanked

Let's check the unnamed register to confirm it worked:

Shorter Method
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
NORMAL
Top
1:1
 
:reg "
Type Name Content
  l  ""   red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f

This method is slightly better than than the previous method, but also moves the cursor to the top of the screen.

Shortest Method

The third, and shortest, method of yanking all buffer content leverages the % range to yank the entire buffer with only 3 keystrokes:

Upon hitting <CR> the entire buffer is yanked:

Shortest Method
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
COMMAND
57%
4:1
:%y

Not only is this the shortest method, it has the additional advantage of keeping the cursor in the same position.

Shortest Method
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
NORMAL
57%
4:1
 
:reg "
Type Name Content
  l  ""   red,#f00green,#0f0blue,#00fyellow,#ff0cyan,#0ffmagenta,#f0f