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:
Common Method
Perhaps the most common method to achieve this task is the following sequence:
-
gg to move the cursor to the top of the buffer.
-
v to enter visual mode.
-
G to move the cursor to the bottom of the buffer.
-
y to yank the selected content.
Let's check the unnamed register to confirm that the full buffer content was yanked:
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:
-
gg to move the cursor to the top of the buffer.
-
y to yank the selected content.
-
G to move the cursor to the bottom of the buffer.
Let's check the unnamed register to confirm it worked:
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:
Not only is this the shortest method, it has the additional advantage of keeping the cursor in the same position.