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
ggmoves the cursor to line 1, column 1 of the file- It is equivalent to
:1<CR>or1G - 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
Gto jump to the last line of the file - Use
42ggor42Gto jump to a specific line number (e.g., line 42) - Combine with operators:
dggdeletes from the current line to the top of the file,yggyanks from the current line to the top - Use
<C-o>to jump back to where you were before pressinggg - Use
gg=Gto auto-indent the entire file (jump to top, indent operator, jump to bottom) - Use
ggVGto select the entire file in visual line mode