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.
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.
Use the G key to jump to the last line. The output shows the cursor at the last line:
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:
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.
$ command shifts the cursor to the end. Switch to normal mode and execute:
Hit Enter and the cursor shifts to the end:
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:
Vim offers several ways to move back to the start of a file. Here are some useful methods.
Getting back to the beginning of a file is quite simple. One way is to type 1G:
Typing gg gives the same output as the previous command:
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:
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.
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:
Hit Enter and the cursor jumps to 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.
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:
|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.|
||Jumps to the first line.|
||Moves the cursor to the end of the file.|
|1+G||Jumps to the top of the file.|
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.