vim

Navigating within a file in VIM

Navigation in a file plays a vital role in text editing. To be more productive, you should know all possible shortcut methods or keys related to file navigation in Vim editor. Navigation within a file in other graphical editors is quite easy and simple. But, in Vim, you must remember some tips and tricks related to Vim navigation options. Vim offers great support to some advanced navigation features.

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:

:edit <filename>

Or you can also type the following command on the terminal to open a file.

$ Vi <filename>

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:

:jumps

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.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.