To perform the command traditionally known as “ctrl + a” to select all in Vim, do ggVG.
Breaking it down:
1. Check that you are in normal mode - hit the ESC key.
2. Move the cursor to the beginning of the file - gg.
3. Enter visual mode which lets you see the highlight portions - V
Pressing V enters line visual mode while pressing lower case v will enter character visual mode. Being in v mode will cause the below command to highlight until the first character of the last line in the file.
4. Select from the current cursor position to the end of the file - G
At this point, you can scroll up and down or use Vim commands to de-select lines.
Vim copy all lines
After performing the select all sequence above, you can use this command to copy the whole entire file: y. This will yank and copy the selected lines.
Vim copy all to clipboard
Using yank alone only copies the portion to the default register (i.e. Vim). It won’t copy to the clipboard. So if you wanted to copy the selected code to use outside the terminal you’ll need to use gg"*yG
- Move the cursor to the beginning of the file - gg
- Copy it to the clipboard from current position - "*y
- Move the cursor to the end of the file - G
Vim paste all
In order to paste what you’ve copied, you can hit p to paste after the cursor and P to paste before the cursor.
Read more about copy/pasting text in Vim.
Vim delete all lines
To clear all the lines of a file: :%d
- Go to normal mode - ESC
- Enter command mode - :
- Select All - %
- Delete - d
Learn more about deleting lines in Vim.
Make a mistake? If you want to undo an accidental deletion, you can use the u command (:u) to undo in Vim, and the lines that were deleted will show up again.
Written by
Glory Kim
Software Engineer, Loom
Filed Under
Related Articles
Vim Find And Replace
You can use either the substitute command or the slash and dot method to search for patterns and replace with different values.
Vim Modes
Learn about seven of Vim’s modes
Vim / Vi Page Up and Down Controls
Quick reference for Vim's page up and down controls
Copy & Paste in Vim / Vi
Copy (Yank), Paste (Put) and Cut (Delete) in Vim
Undo & Redo in Vim / Vi
Keyboard shortcuts and summary of how Vim tracks changes
Show & Hide Line Numbers in Vim / Vi
Toggle absolute and relative line numbers
Searching in Vim
Search forward, backward, case insensitive, and more
Go To End of File in Vim / Vi
Select and delete to the end of a file in Vim
How to delete lines in Vim / Vi
The best answer depends on which mode you use