How to Select All in VIM

Learn how to select an entire file in VIM as well as copy and delete the contents.

How to Select All in VIM

It’s time to upgrade your VIM skills by learning to perform the “Select All” operation. By understanding the following commands, you will also learn how the different visual modes work and how to use the Ex command to help copy entire file contents quicker. 

How to Select All in VIM

To perform a “Select All” operation in VIM, use the ggVG command.

VIM Select All

Make sure that you start this command from NORMAL mode. Hitting gg will take you to the top of the file. Hitting V (capital) will put you into VISUAL LINE mode. Finally, hitting G (capital) will move the cursor to the last line in the file, therefore selecting all of the contents of the file.

When entering VISUAL mode, make sure to hit capital V. Lowercase v will put you into VISUAL CHARACTER mode, which will only select the first character of the last line when you hit capital G

The most common “Select All” follow-up command is “copy.” Here is how to select all and copy in Vim.

Select All and Copy in VIM

To copy or “yank” all of the selected lines after the ggVG command, you can hit y. Adding “y” makes the full command ggVGy. However, there is a shorter method of copying the entire file.

:%y

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

Besides being shorter, a nice benefit of this command is that your cursor will not be moved, and you will remain in NORMAL mode after the command runs.

Select All and Delete in VIM

If you would like to delete the contents of your file, you can use the copy command but replace the yank operation with delete.

:%d

Remember that deleting with the d operator will copy the contents of the selection into the global register. If you don’t want your clipboard overwritten, make sure to delete into a different register. We can learn more about how to delete with a register in our guide here.

For more helpful VIM guides, please check out our VIM section.