Select All Text in Vim

Let's discuss how you can select everything in Vim. It can be easily done with a 4 character command!

Following are the keys to press (in Normal mode) to select everything in Vim:

ggVG

This will select everything from the first character in the first line to the last character in the last line. Then, you can either press the y key to yank (copy) or d key to delete it. This is similar to pressing the Ctrl + a key combination in many mouse-oriented text editors.

If you want to understand how this works, please keep reading!

Selecting text in Vim

The text selection in Vim is done by being in the VISUAL mode. There are three ways that you can select text:

  1. v: Select individual characters
  2. V: Select entire lines
  3. Ctrl + v: Block selection

Understanding the above Vim tip

Now that you know the three methods of selecting text in Vim, let us now understand how the above key combination of ggVG works!

When you are in normal mode and press gg, you are taken to the first character on the first line of the text file that you are ediitng. Then, when you press the V key, you enter the Visual mode wherein you select entire lines of text. Finally, when you press the G key, you are taken to the end of document.

Following is another representation of what happens:

  1. gg: Move cursor to the top
  2. V: Start selecting entires lines (from the top, because of gg)
  3. G: Move cursor to the last line, selecting everything

Following is a demonstration of this:

As you can see, I am on line 100 when I press the gg key combination to reach at the top of the document. The key combination is visible before the utf-8 string on the bottom. You can also see that the position changed from 23% to Top, validating that I have indeed reached to the first line.

Then, I press the V key to start selecting. At that moment, the first line gets selected. Then, I press the G key to move the cursor to the last line and you can see the lines being selected as well, as the cursor moves down, at the bottom.

Finally, to prove that all lines have been selected, I delete them using the d key, which results in everything being deleted.

More Vim text selection tips

Now that you know how to select everything, let's also look at some tangentially useful Vim key combinations!

  1. vy: Select an entire line where the cursor is placed (even if it is word wrapped)
  2. vw: Starting from the cursor's position, select to the beginning of the next word
  3. ve: Starting from the cursor's position, select to the end of current word
  4. v$: Starting from the cursor's position, select to the end of line (even if it is word wrapped)
  5. v0: Starting from the cursor's position, select to the start of the line (even if it is word wrapped)
  6. V<num>k: Starting from current line, select <num> lines above. So V10k selects current line and 10 more lines above.
  7. V<num>j: Same as V<num>k, but in the opposite direction: selecting lines downwards.

Conclusion

In this short article, we cover how you can not only select all text in Vim, but I also cover some more tips regarding selecting text. Happy Vim-img!