How To 'Select All' in Vim

The Linux, BSD, and macOS operating systems all come with Vim, a Unix text editor. Its reputation for speed and effectiveness is partly due to the fact that it is a simple application that can run in a terminal (although it also has a graphical user interface), but mostly because it can be used exclusively with the keyboard without the need for a mouse or menus. Contrary to what most current computer users would anticipate, this kind of text editing is used by Unix administrators worldwide to alter configuration files, changelogs, scripts, and other things.

Related: Vim cheatsheet

Vim Cheatsheet
Complete list of commands and shortcuts to navigate around the world of Vim / Vi.

There are a couple of different ways you can select all (& copy) the contents of your vim or vi (as it is also commonly known as) text editor. Some of these are outlined below.

Method 1: Select All

The easiest way to select all in your vim editor is by typing ggVG into your terminal. Here,

  • gg moves to the first line of the editor
  • V opens up the visual mode
  • G jumps to the last line and selects everything from the first to the end

Make sure you're in normal mode before you use this command. If you are not, you can press the ESC key to change normal mode and then use the ggVG command to select all.

Use the ggVG command to select all in the Vim editor
Use the ggVG command to select all in the Vim editor

Method 2: Using a Big Number To Select All

One hacky way to select everything in your editor is to by supplying the line count.

If you supply a huge number such as 99999, you will be able to select everything in your editor. First, use the ESC key to go to regular mode. After that, use gg to go to the beginning of the file. From here, you can use the 99999yy command to copy all the way to the end of the file. The yy at the end will yank (or copy) everything that has been selected.

Method 3: Select All & Copy

There are a couple of ways you can select all and copy from your vim editor. One of them is by using the gg"*yG command in normal mode. Here,

  • gg as previously noted, will move the cursor to the first character of the file
  • "*y will start a yank command to the vim register from the first line of the file where your cursor is
  • G the yank command will stop at the end of the file (G jumps to the last line and selects everything in the process)

Related: How to comment out multiple lines in Vim

Vim: How to Comment Multiple Lines
There are multiple different ways you can write a multiline comment in the vim/vi editor. Here are some of the most commonly used ones.

Bonus: How To Select Some Text in Vim

If you don't want to select all your text, but only some of it, there are multiple ways to do this. Text in vim is selected by entering visual mode.

  • v (lowercase) opens regular visual mode, and the workings of it are similar to selecting text with a standard mouse. You can use the h and l keys to expand the selection left and right respectively if you want to include more words. Similarly, you can use the j and k keys to include the lines above and below the line you are currently on.
  • V (upper case) is used to enter linewise visual mode. In this mode, you can select entire lines of text in one go. You can use j and k to expand the selection up and down as mentioned above.
  • Ctrl+v~ will open up block visual mode. Here, you will be able to select text in a block format. This will let you select parts of multiple lines without selecting the entire line. The h, j, k and l keys can be used as mentioned above to select more parts of your text.

Useful Vim Commands After Selecting Your Text

Once you have selected the text you want, you can use all sorts of commands on them. Some of the more useful ones are:

  • esc Press the escape button to exit visual mode
  • d will delete the selected text
  • y is used to yank (copy) the text
  • p is used to paste what is on your clipboard onto the text you have selected; this will replace it
  • c can change the text - this means that the text gets deleted and your cursor is set for typing
  • r is used to replace the text with the next character you type

Vim is known for its efficiency, and while it might look daunting in the beginning, you can pick up the most commonly used commands and get proficient in less than a day!