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:
v
: Select individual charactersV
: Select entire linesCtrl + 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:
gg
: Move cursor to the topV
: Start selecting entires lines (from the top, because ofgg
)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!
vy
: Select an entire line where the cursor is placed (even if it is word wrapped)vw
: Starting from the cursor's position, select to the beginning of the next wordve
: Starting from the cursor's position, select to the end of current wordv$
: Starting from the cursor's position, select to the end of line (even if it is word wrapped)v0
: Starting from the cursor's position, select to the start of the line (even if it is word wrapped)V<num>k
: Starting from current line, select<num>
lines above. SoV10k
selects current line and 10 more lines above.V<num>j
: Same asV<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!