Going to the End & Start of File in Vim
2/27/2024
TLDR
To ensure that the following commands work as expected, Vim needs to be in normal mode. You can switch to this mode from the editing mode by pressing the Esc
key.
Command | Description |
---|---|
G | Moves to the last line of the file. |
gg | Moves to the beginning of the file. |
]] | Moves the cursor to the end of the file. |
[[ | Moves the cursor to the start of the file. |
G + A | Goes to the end of the file and switches to the Insert mode. |
gg + A | Shifts to the top of the file and switches to the Insert mode. |
Ctrl + Home | Displays the top of the text. |
Ctrl + End | Displays the very end of the text. |
:1 | Jumps to the first line. |
:$ | Moves the cursor to the end of the file. |
What we'll learn
In Vim, the ability to jump directly to the start or end of a document can make your editing significantly faster. In this guide we'll cover:
- Different methods to jump to a file's beginning or end
- Tips for using Vim's modes to speed up navigation
- A summary of key commands for immediate file navigation
Jumping to the End of a File in Vim
Vim offers several shortcuts to jump straight to the bottom of a file. In this section, we'll discuss five easy options to reach the end of a file in Vim.
Option 1: Moving to the Last Line
You can use the Shift + G
keys to quickly jump to the last line of the file. This command places your cursor at the start of the last line.
Option 2: Using the End Key
While the G
key takes you to the last line, it doesn't move the cursor to its end. To jump right to the end of the current file, you can combine Ctrl + End
keys.
Option 3: Inserting Text at the End
To navigate to the bottom of the file and enter the Insert mode, use the G + A
key combination. Unlike just pressing G
, this command takes the cursor to the very end, exits the normal mode, and lets you start adding, editing, or removing text immediately.
Option 4: Using the $ Command
Activate the $
Command to move the cursor directly to the end. Start by making sure that you're in the Normal mode, and then execute this command. The command syntax is :$
. Press Enter
and the cursor will move right to the end of the file.
Option 5: Applying the Double Closed Brackets
In the Normal mode, entering the double closed brackets (]]
) will shift the cursor to the end. In this case, it's not necessary to hit the Enter
key.
Returning to the Top of a File in Vim
Vim offers several simple options to return to the file's beginning quickly. Let's explore some of the most effective ones.
Option 1: Navigating to the Beginning of the File
Perhaps the easiest way to go to the start of a file is to use the 1G
command.
Option 2: Using the gg Command
Just type gg
to get the same effect as the first option; it will return your cursor at the beginning of the file.
Option 3: Using the Home Key with Ctrl
You can also go to the top by pressing the Home
key. This works similarly to jumping to the end, but you must use it along with the Ctrl
key. Press Ctrl + Home
to go to the beginning of the document.
Option 4: Jumping to the Start with Square Brackets
As you jump quickly to the bottom of a file with double-closed brackets, you can also move quickly to the start of a file with double-open brackets. Start by pressing Esc
to change to the Normal mode. and type [[
. Once you enter both brackets, the cursor will jump to the top.
Option 5: Navigating to the First Line
Using line numbers is a convenient option to reach the beginning of a file. While it's not the best option for getting to the end, the first line is always Line 1. As such, you can use this command to go to the very first line quickly. :1
. Hit Enter
, and the cursor jumps to the beginning.
Option 6: Inserting Text at the Start
Use the gg + A
command to move to the first line of the file and switch to the Insert mode for immediate editing.