In Vim, to select all the text in a file, use the ggVG command in NORMAL mode. After selecting the text, operations like copying, deleting, changing, or pasting can easily be performed on all the text.
If you are using a GUI-based operating system, then you must be familiar with the ctrl+a shortcut keys to select all the content in a file or window. In Vim, you cannot use the same keys to select all text. Instead, it offers a different set of keys (ggVG) to select everything in a file. In this guide, I will be exploring how to select text in Vim, and how to make it easier using key-mapping.
Note: Vim Commands mentioned in this guide are performed on macOS. Since Vim is a cross-platform text editor, the commands will work without any error on Linux and Windows as well.
Select All in Vim
To select all in Vim, press the Esc key to enter the NORMAL mode, and type the command given below:
In the above command, a combination of five keys is used. Let’s break it down.
- gg is used to put the cursor at the start of the first line.
- V is used to switch the current mode to the VISUAL LINE mode.
- G is used to put the cursor at the start of the last line.
After selecting all, use the commands mentioned in the following table to perform the desired operation.
y | To copy(yank) all the text |
d | To delete all the text |
c | To change all the text |
s | To substitute a specific pattern with a string |
Select All in Vim using ctrl+a Map
Well, including me, nobody would like to use a bit tough-to-remember set of Vim commands to select all the text in a file. However, the ctrl+a (on Linux and Windows) keys are universally used and easy to remember. Therefore, I would prefer to map the ctrl+a keys with the ggVG command.
Open the vimrc file, place the following line in it, and save it by using the :wq command or pressing the shift+zz keys.
In the above command, nnoremap is used to map the key non-recursively in the NORMAL mode while <C-a> indicates the ctrl, and a keys. You can set any key as per your preference.
Copy and Delete All the Lines in a File
The % command is another useful command to perform operations on the entire file. For example, to copy all the lines in the file, use the :%y command. The % indicates the current file name, and y is copying all the text in the file. Similarly, to delete all the lines, use the :%d command.
:%d "Delete all the lines in a file
:%s/<pattern>/<replacement> "Substitute a pattern with a string
The same functionality can also be achieved using the gg and G commands. But it depends upon the cursor position. Bring your cursor at the beginning of the file using gg and press yG to copy all the lines. In the same way, place the cursor at the end using G and press ygg to copy all the lines. To delete the lines, replace y with d.
Furthermore, the yy and dd commands also take count as a line number. To copy four lines, press 4yy, and four lines from the cursor position will be copied. The copy and delete all in-file functionality can be achieved if we pass a big number such as 99999 with either yy or dd command. For example, 99999yy and 99999dd commands will copy and delete all in-file content.
Copy All the Text to the System Clipboard
To paste the copied text from Vim to system applications such as text editors or browsers, use the gg“*yG command. Typically, Vim copies the text to the default unnamed register, which can be accessed using :reg “”. However, Vim will not allow you to paste the text outside the Vim editor. Use the “*y command, to transfer the selected text to the operating system clipboard.
In the above command, * is a system register, which is a small storage space to hold data. There are two system registers + and * and both can be used to copy content to the system clipboard. You can use this command differently when you are in COMMAND mode.
Here, % represents the entire file, y is for yanking, and + is the system register. However, * can also be employed for the same purpose.
Similarly, if you have copied something on the clipboard, to paste it in Vim use, “+p.
Conclusion
In Vim, the command ggVG is used to select all in a file. After selecting all the lines, various operations can be performed, such as copying, changing, or deleting. However, these operations can also be performed using the command line with the % sign. I recommend mapping a key (keys) to perform the select-all operation in Vim.