Select all in Vim / Vi

Last Updated 2022-12-18

To perform the command traditionally known as “ctrl + a” to select all in Vim, do [.inline-code]ggVG[.inline-code].

Breaking it down:

1. Check that you are in normal mode - hit the [.inline-code]ESC[.inline-code] key.

2. Move the cursor to the beginning of the file - [.inline-code]gg[.inline-code].

3. Enter visual mode which lets you see the highlight portions - [.inline-code]V[.inline-code]

Pressing [.inline-code]V[.inline-code] enters line visual mode while pressing lower case [.inline-code]v[.inline-code] will enter character visual mode. Being in [.inline-code]v[.inline-code] 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 - [.inline-code]G[.inline-code]

At this point, you can scroll up and down or use Vim commands to de-select lines.

[#copy-all-lines]Vim copy all lines[#copy-all-lines]

After performing the select all sequence above, you can use this command to copy the whole entire file: [.inline-code]y[.inline-code]. 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 [.inline-code]gg"*yG[.inline-code]

  • Move the cursor to the beginning of the file - [.inline-code]gg[.inline-code]
  • Copy it to the clipboard from current position - [.inline-code]"*y[.inline-code] 
  • Move the cursor to the end of the file - [.inline-code]G[.inline-code]

[#paste-all]Vim paste all[#paste-all]

In order to paste what you’ve copied, you can hit [.inline-code]p[.inline-code] to paste after the cursor and [.inline-code]P[.inline-code] to paste before the cursor.

Read more about copy/pasting text in Vim.

[#delete-all-lines]Vim delete all lines[#delete-all-lines]

To clear all the lines of a file: [.inline-code]:%d[.inline-code]

  • Go to normal mode - [.inline-code]ESC[.inline-code]
  • Enter command mode - [.inline-code]:[.inline-code]
  • Select All - [.inline-code]%[.inline-code]
  • Delete - [.inline-code]d[.inline-code]

Learn more about deleting lines in Vim.

Make a mistake? If you want to undo an accidental deletion, you can use the [.inline-code]u[.inline-code] command ([.inline-code]:u[.inline-code]) to undo in Vim, and the lines that were deleted will show up again.