In this article, we will explain how to navigate within a file using Vim. We will suggest some navigation techniques for navigation, which will help you to improve productivity.
We have run all navigation options on Vim in this article using Ubuntu 20.04 system.
How to navigate within a file in Vim?
Vim offers some complicated ways to navigate within a file. You have to remember shortcut keys for Vim navigation. Let us start reviewing the following Vim navigation options.
First, you will open the terminal through the application launcher bar. Now, type ‘Vi’ to launch the Vim command-line editor. Type the following command to open a file in Vim for editing:
Or you can also type the following command on the terminal to open a file.
Navigating within file lines
Once the file is open in Vim, you will try the following file line navigation options on this file.
k – move upward
j – move downward
l – move to the right side
h – move to the left side
You can also use keyboard arrow keys for navigation in a file.
You can also repeat the operation for N times in Vim. For example, if you want to move down by 5 lines, then write ‘5j’.
Vim also allows users to navigate in a file to other positions instead of only the upward or download. Use the following options to do this action.
0 – cursor moves to the start of the current line
$ – cursor moves to the end of the current line
Ctrl + f – move to scroll down the entire page
Ctrl + b – move to scroll up the entire page
Navigating between the file words
You may use the following command for word navigation within a file −
w – cursor moves to the start of the next word
e – cursor moves to the end of the word
b – cursor moves to the start of the previous word
Special navigation
The following commands can be used to navigate to a particular line position:
:n – move to the nth line in a file
:0 – move to the start of the current file
or
(gg – this option will also move the cursor to start of the file)
:$ – move to the end of the current file
or
(G – you may also use this option to move to the end of the file)
Note: when you will use keyboards keys, then be careful about the case of keys.
H – move the cursor to the start of the displaying screen.
M – move the cursor to the middle line of the screen.
L – move the cursor to the end line of the screen.
Navigating within Paragraphs
{ – move the cursor to the start of the current paragraph. If you press { again and again, this will move you to the start of the previous paragraph.
} – move the cursor to the end of the paragraph. If you press } again and again, it will move you to the end of the next paragraph.
How to use jumps for navigation?
Vim keeps a record of each navigation through a jump list. You can move backward and forward directions using the jump list.
The jump list keeps the record of all places you have been visited by tracking the line number, column, and the file/text.
Execute the following command to display the Jump list:
You may use the following command based on your jump list −
Ctrl + o – Jump the cursor to the previous position
Ctrl + i – Jump the cursor to the next position
Code Navigation
You can use the following options while you are working on programming code files.
% cursor will move to match parenthesis
[[ move to start of the function
[{ move to the start of the block
That is all about navigation within a file in Vim editor. In this article, we explained various navigation options and commands which will help you in the future. You can explore more navigation shortcuts using Vim. I hope you like this article.