Go to Beginning or End of File in Vim
If you open a very long file in Vim, it can get tough to navigate the file.
It is good to know how to move the cursor from anywhere in the file to either the first line or to the last line.
Move | Keys |
---|---|
To the beginning | gg or [[ |
To the end | shift + g or ]] |
You need to be in normal mode (also known as command mode) to use these keyboard shortcuts. Use the Esc
key to enter the command mode, and then use the above-mentioned keys.
Let's go a bit in detail.
Jump to the last line in Vim
Just like Apple's phrase "There's an app for that", in Vim, "There's a command for that".
It does not matter which row or column you are in Vim, to move your cursor to the last line, simply press an uppercase G
(i.e. Shift and g key together) in Normal mode, and your cursor will move to the first character of the last line.
Another way to jump to the end of a file is by using the Esc + Ctrl + End
or ]]
key combination.
Jump to the first line in Vim
There are several ways you can jump to the first line in Vim.
Since these are Vim commands, make sure that you are in Normal mode (use Esc key) before pressing any keys.
To jump back to the first line of the file, you can use either one of the following commands:
1G
This command tells vim to move the cursor to line 1, as specified in the command.
gg
Typing gg
has the same impact on cursor location - the cursor is moved to the first column of the first row.
I prefer using gg
instead of 1G
as it is easy to type 2G
and end up on the second line.
Instead of that, you can also use the Esc + Ctrl + Home
key combination to move your cursor to the first line of any file.
Conclusion
This article covers how you can move your cursor from anywhere in the file to the beginning or end of the file.
If you are interested in learning more than just the Vim Basics, I highly recommend using this training course by Jovica Ilic.