vimtricks.wiki Concise Vim tricks, one at a time.

How do I select or operate on the entire buffer as a text object?

Answer

ggVG

Explanation

While Vim doesn't have a built-in "entire buffer" text object, the ggVG sequence achieves it: go to the first line, enter line-wise visual mode, then select to the last line. This is the idiomatic way to select everything.

How it works

  • gg moves to the first line
  • V enters line-wise visual mode
  • G extends the selection to the last line

Common patterns

" Select entire buffer
ggVG

" Delete entire buffer
ggdG

" Yank entire buffer
ggyG      " or :%y

" Reindent entire buffer
gg=G

" Format entire buffer
ggVGgq

Alternative: Using ranges

Many operations work more cleanly with % (the whole-file range):

:%d         " Delete all lines
:%y         " Yank all lines
:%s/a/b/g   " Substitute in all lines
:%normal A; " Append to all lines
:%!sort     " Sort all lines

Creating a real text object

Many users add a custom "entire" text object to their vimrc:

" 'ae' for entire buffer (like 'iw' for inner word)
onoremap ae :<C-u>normal! ggVG<CR>
vnoremap ae gg0oG$

Now you can use dae (delete all), yae (yank all), =ae (reindent all).

Tips

  • Use % in Ex commands instead of ggVG for efficiency
  • The gg=G (reindent all) is one of the most frequently used buffer-wide operations
  • ggVGp replaces the entire buffer with the register contents

Next

How do I always access my last yanked text regardless of deletes?