1. Overview

Vim is one of the most widely used editors. This wide acceptance comes from its support for different kinds of actions and motions.

In this article, we’ll look at the different ways we can navigate a file using Vim.

2. Motions

Motions are basically how we move around in a file using the Vim editor. The Vim editor supports different motions in normal mode.

Let’s look at some of them:

Keys Movement
j Move down
k Move up
h Move left
l Move right
w Move forward over words
b Move backward over words
( Move backward over sentences
) Move forward over sentences
{ Move backward over paragraphs
} Move forward over paragraphs
H Move to the first line (home) in the window
M Move to the middle line in the window
L Move to the last line in the window
/ Search forward for text
? Search backward
n/N Find the next or previous occurrence respectively
nG Go to the nth line

In the table above, we can see the different keys and the motions induced by them.

This moves the cursor around the file for editing or reading the file.

In Vim, some motions are special and they get registered as marks. Some motions are jumps. We’ll look at marks and jumps in the next sections.

3. Marks

Marks allow us to set bookmarks in the document. We can use the letters from a-z to refer to a mark. Later, we can go to a particular mark by mentioning the letter.

For example, let’s say we’re on line 10 in a file. We may want to set a bookmark there so that we can come back to it later.

We can set the mark using the keys: ma

Now the mark is set on line 10. After that, we can move around the file. Finally, when we want to go back to the place where we set a mark, i.e., to line 10, we can do it by typing: ‘a or `a

The first one takes us to the line where we set the mark and the second goes to the exact column in the line.

We can also use uppercase letters from A-Z to store the marks. These work across files. Let’s say we’re on file test1.txt and we set a mark using the key combination mA. Now we open another file test2.txt. From the new file if we want to go back to test1.txt we can simply type ‘A or `A to go there. It really helps when we’ve to switch between different files quickly.

Furthermore, if we wanted to see the list of marks we can execute the :marks command. Other than this, by default, there won’t be any visual indication of a mark in a file.

3.1. Special Marks

As we saw above, these marks are manually set by us. But there are some marks that are set automatically by the editor. Typically, when we jump between locations or make some changes to the file, those locations are remembered. We can go to those specific locations using the designated key combinations.

Let’s look at some of these.

3.2. Go to the Last Insert

We can use these keys to go to the place where the last insert was completed: ‘^ or `^

By doing this, we’re taken to the place where the insert was last stopped.

3.3. Go to the Last Change

Here’s how we can go to the last place where we made a change: ‘. or `.

This key combination takes us to the place where we last edited.

4. Jumps

We can say that jumps are a special kind of motions. When we move around the file, some of the positions get registered by Vim in a list. Later we can go back in the list one after the other. We can use Ctl-o and Ctrl-i key combinations for that. The former goes backward in the list and the latter takes us to the recent ones.

The jump list gets updated only for some particular motions. To confirm that, let’s open a text file in Vim. Then, we use the j/k keys to move around. Now, if we try to go back to older positions using the Ctrl-o key, it only goes to the initial position when the file was opened. Thus, we know movement using the j/k keys doesn’t get registered in the jump list.

We’ve already seen some motion commands earlier. Out of those, the commands starting from ( create a jump list. For the complete list of commands, we can check the help in Vim by entering the :help jump command.

The jump list is saved across sessions. If we’ve opened other files earlier and invoked commands which created jump lists, we can go to those files when we navigate backward using the Ctrl-o key.

And when we want to see the list, we can type the command :jumps

5. Changes

Not only motions, but also changes are stored in a list. It’s called the change list. This provides another way to navigate through the changes we made in a file.

To see the change list, we can type the command :changes

And to navigate through them we can use the g commands.

To move backward, we can use the keys: g;

Then, to move forward we can use the keys: g,

Incidentally, the move backward command takes us to the position of the last edit.

6. Conclusion

In this article, we’ve taken a look at different motions in Vim. And we’ve seen attached to the motions there are marks, jumps, and changes. We’ve also learned some handy ways to navigate through these locations in a file.

Comments are closed on this article!