Back to articles

Select All in Vim

Computer TechDevelopmentEditorsNeovimSelect All in Vim
Updated 4/23/2026

Select All in Vim

The Classic Method: ggVG

The most common way to select all text in Vim:

ggVG

Breakdown:

  • gg - Jump to first line
  • V - Enter Visual Line mode
  • G - Jump to last line (extends selection to end of file)

This command uses Visual Line mode (V), which selects entire lines. See Visual Mode in Vim for detailed explanation of visual selection modes.

Why Type g Twice?

In Vim, many commands are composed of multiple keystrokes for precision and consistency:

Single g is a prefix key - It doesn't do anything by itself. It signals "I'm about to give you a motion command."

Common g commands:

  • gg - Go to first line (line 1)
  • gj - Move down one display line (in wrapped text)
  • gk - Move up one display line (in wrapped text)
  • gf - Go to file under cursor
  • gd - Go to definition

Why this design?

  • Single G - Already taken! It means "go to last line" (or go to specific line if you type a number first like 42G)
  • Consistency - Many Vim commands use this pattern: gg (first line), G (last line), 0 (start of line), $ (end of line)
  • Discoverability - The g prefix groups related navigation commands together

Alternative way to think about it:

  • gg = "go to the beginning"
  • G = "go to the end"
  • So ggVG = "beginning, then select, then end"

The Pattern in Action

This two-keystroke pattern appears throughout Vim:

CommandMeaningWhy Two Keys?
ggFirst lineg prefix + g for "go"
ddDelete lined operator + d for "current line"
yyYank (copy) liney operator + y for "current line"
ccChange linec operator + c for "current line"
>>Indent line> operator + > for "current line"

The rule: Doubling a command often means "apply to current line" or creates a specific motion.

Why Visual Line mode for "select all"?

  • Code operations typically work on complete lines
  • Line-wise paste maintains proper formatting
  • Clear visual feedback shows selected line range

Alternative Selection Methods

Character-Wise Selection

ggvG

Uses lowercase v for character-wise visual mode. Selects from first character to last, including partial lines.

Block Selection (Columns)

gg<C-v>G$

Uses ⌃V for visual block mode. Selects rectangular column from first to last line.

Comparison:

  • ggVG - Lines (most common, whole lines)
  • ggvG - Characters (precise, can be partial lines)
  • gg<C-v>G$ - Columns (rectangular selection)

Select All and Copy to Clipboard

:%y+
  • % - All lines in file
  • y - Yank
  • + - System clipboard register

Or use the classic method:

ggVG"+y

Select All and Delete

:%d

Or:

ggdG
  • gg - Go to top
  • dG - Delete to end of file

Operations After Selection

After selecting text with ggVG, apply operations:

KeyActionResult
yYank (copy)Copies all lines to register
dDeleteRemoves all lines
cChangeDeletes all and enters insert mode
=Auto-indentReformats all lines
>Indent rightIndents all lines
<Indent leftUn-indents all lines
:Ex commandRuns command on selection

See Visual Mode in Vim for complete list of visual mode operations.

Common Use Cases

Copy Entire File

:%y+        " Copy to system clipboard
:%y         " Copy to default register

Replace Entire File

ggdG        " Delete all
i           " Insert mode
<paste>     " Paste new content

Or:

:%d         " Delete all lines
<paste>     " Paste in normal mode

Auto-indent Entire File

ggVG=

Or:

gg=G

Transform Entire File

ggVG:s/old/new/g   " Find and replace in all lines

Or use the simpler:

:%s/old/new/g
  • ggVG - Select all lines
  • ggvG - Select all characters
  • :%y+ - Copy all to clipboard
  • :%d - Delete all
  • gg=G - Auto-indent all
  • :%s/old/new/g - Replace in all lines

Comparison: Vim vs Other Editors

ActionVimVS CodemacOS
Select allggVG⌘A⌘A
Copy all:%y+⌘A then ⌘C⌘A then ⌘C
Delete all:%d or ggdG⌘A then ⌫⌘A then ⌫

Why Not Just Use ⌘A?

In some Vim contexts (like Neovim), you can map <D-a> (⌘A) to select all:

lua
vim.keymap.set('n', '<D-a>', 'ggVG', { desc = 'Select all' })

But the classic ggVG works everywhere: terminal Vim, SSH sessions, and any Vim-like editor.

Official Vim Documentation