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

How do I jump to the beginning of a file in Vim?

Answer

gg

Explanation

The gg command moves the cursor to the first line of the file. It is one of the most essential navigation commands in Vim and is used constantly when editing files.

How it works

  • gg moves the cursor to line 1, column 1 of the file
  • It is equivalent to :1<CR> or 1G
  • The jump is recorded in the jump list, so you can return to your previous position with <C-o>

Example

No matter where your cursor is in a file — line 500, line 2000, or anywhere else — pressing gg instantly takes you to the very first line.

Tips

  • Use G to jump to the last line of the file
  • Use 42gg or 42G to jump to a specific line number (e.g., line 42)
  • Combine with operators: dgg deletes from the current line to the top of the file, ygg yanks from the current line to the top
  • Use <C-o> to jump back to where you were before pressing gg
  • Use gg=G to auto-indent the entire file (jump to top, indent operator, jump to bottom)
  • Use ggVG to select the entire file in visual line mode

Next

How do I edit multiple lines at once using multiple cursors in Vim?