Introduction
When working on a large file in Vim, users often need to move around to edit different sections. While a keyboard is efficient with short texts, it’s not the best choice for extensive documents.
Vim is equipped with functional tools for navigating big files, including jumping to the top or bottom of a file.
This guide will show you how to go to the end or top of a file in Vim.
Note: To execute commands, Vim needs to be in normal mode. Press Esc to switch to normal mode.
How To Jump to the End of the File
Moving to the end of a file is slow when using arrow keys only. Luckily, Vim offers several ways to quickly reach the bottom of a file.
Note: Commands in Vim are case sensitive.
1. Jump to the Last Line
Use the G key to jump to the last line. The output shows the cursor at the last line:
2. Use the End Key
The G key moves the cursor to the final line but not to the end of that line. So, to jump to the end of a line, use the Ctrl and End keys:
3. Append/Insert Text at the End
Move to the bottom of the file and switch to insert mode with G + A.
Unlike G, this command sends the cursor to the end and allows users to add, edit, or delete text.
4. Run the $ Command
The $
command shifts the cursor to the end. Switch to normal mode and execute:
:$
Hit Enter and the cursor shifts to the end:
5. Use the Closed Brackets
Typing the double-closed brackets when in normal mode moves the cursor to the end:
The terminal displays the last line as soon as the user types ]] - there is no need to hit Enter in this case:
How To Jump Back to the Top of the File
Vim offers several ways to move back to the start of a file. Here are some useful methods.
1. Go to the Beginning of the File
Getting back to the beginning of a file is quite simple. One way is to type 1G:
2. Use the gg Keystroke
Typing gg gives the same output as the previous command:
3. Use the Home Button
Another way to jump to the top is to use the Home button. Much like jumping to the end, this key only works when combined with Ctrl.
Use Ctrl + Home to perform this action:
4. Jump to the Start with Open Brackets
Move to the start of a file quickly with double-open brackets. Hit Esc to switch to normal mode and type [[:
After typing both brackets, the cursor jumps to the top.
5. Go to the First Line
Moving through text using line numbers is a practical way to jump to the start of a file. However, this is not an efficient way to jump to the end of a file.
But, the first line is always Line 1. Therefore, use this command to move the cursor to the first line instantly:
:1
Hit Enter and the cursor jumps to the beginning:
6. Append/Insert Text at the Beginning
Move to the top and start editing the text with gg+A. This key combo moves the cursor to the first line and enters you into insert mode.
Moving Through a File in Vi/Vim - Overview
Knowing how to move in Vim and jump to the top or bottom of the text is useful when handling large documents.
Make sure to hit the Esc key to switch to normal mode before running any command. The table below shows all the ways to go to the beginning or end of a document in Vim:
Option | Description |
---|---|
G | Jumps to the beginning of the last line of the file. |
gg | Shifts to the beginning. |
]] | Moves the cursor to the end. |
[[ | Moves the cursor to the start. |
G + A | Goes to the end of the file and switches to the insert mode. |
gg+A | Shifts to the top 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. |
1+G | Jumps to the top of the file. |
Conclusion
This tutorial explained how to move to the top or end of the file in Vim. Now you know how to navigate a text in Vim using different methods.
Next, learn how to save a file in Vim and exit the text editor.