Now is the time to improve your VIM skills by learning how to use “Select All.”
By understanding the following commands, you will also learn how the different visual modes work and how to use the Ex command to copy the whole contents of a file faster.
Learn How to Select All in VIM?
To perform a “Select All” operation in VIM, use the ggVG command.
Make sure you start this command in NORMAL mode. If you press gg, you’ll go to the top of the file. If you press V (capital V), you’ll go into VISUAL LINE mode. Lastly, pressing G (capital) will move the cursor to the last line in the file, selecting the whole file’s contents.
Make sure to hit capital V when going into VISUAL mode. Lowercase v will put you in VISUAL CHARACTER mode. When you hit capital G, you can only select the first character of the last line.
Most of the time, the next command after “Select All” is “copy.” Here is how to copy everything that is selected in Vim.
How to Select All and Copy in VIM?
After the ggVG command, you can hit y to copy or “yank” all of the selected lines. When you add the “y,” the full command is ggVGy. There is, however, a faster way to copy the whole file.
Remember, you will need to hit Enter for this command to execute. Here is the breakdown of the command
- : – Enter Ex Command Mode
- % – Make the next command operate on the entire file
- y – Yank the selection
In addition to being shorter, this command is nice because your cursor won’t move and you’ll stay in NORMAL mode after it runs.
How to Select All and Delete in VIM?
If you want to delete the contents of your file, you can use the copy command but change the yank operation to delete.
When you use the d operator to delete, the contents of the selection are copied into the global register. Make sure to delete it into a different register if you don’t want your clipboard to be overwritten.