Select All in Vim
Select All in Vim
The Classic Method: ggVG
The most common way to select all text in Vim:
ggVG
Breakdown:
gg- Jump to first lineV- Enter Visual Line modeG- 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 cursorgd- 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 like42G) - Consistency - Many Vim commands use this pattern:
gg(first line),G(last line),0(start of line),$(end of line) - Discoverability - The
gprefix 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:
| Command | Meaning | Why Two Keys? |
|---|---|---|
gg | First line | g prefix + g for "go" |
dd | Delete line | d operator + d for "current line" |
yy | Yank (copy) line | y operator + y for "current line" |
cc | Change line | c 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 filey- Yank+- System clipboard register
Or use the classic method:
ggVG"+y
Select All and Delete
:%d
Or:
ggdG
gg- Go to topdG- Delete to end of file
Operations After Selection
After selecting text with ggVG, apply operations:
| Key | Action | Result |
|---|---|---|
y | Yank (copy) | Copies all lines to register |
d | Delete | Removes all lines |
c | Change | Deletes all and enters insert mode |
= | Auto-indent | Reformats all lines |
> | Indent right | Indents all lines |
< | Indent left | Un-indents all lines |
: | Ex command | Runs 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
Related Commands
ggVG- Select all linesggvG- Select all characters:%y+- Copy all to clipboard:%d- Delete allgg=G- Auto-indent all:%s/old/new/g- Replace in all lines
Comparison: Vim vs Other Editors
| Action | Vim | VS Code | macOS |
|---|---|---|---|
| Select all | ggVG | ⌘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:
luavim.keymap.set('n', '<D-a>', 'ggVG', { desc = 'Select all' })
But the classic ggVG works everywhere: terminal Vim, SSH sessions, and any Vim-like editor.
Links
Official Vim Documentation
- URL: https://vimhelp.org/visual.txt.html
- Summary: Official documentation for Visual mode selection commands
- Related: Vim Keyboard Reference, Neovim - The Vim Ecosystem
Related Articles
- Visual Mode in Vim - Complete guide to visual selection modes
- Vim Keyboard Reference - Full keyboard command reference
- Neovim - The Vim Ecosystem - Modern Vim setup and plugins
- LazyVim Setup - Preconfigured Neovim distribution