Introduction

One key spirit of Vim is to accomplish something in as fewer key strokes as possible. When you repeat a command a few times in order to perform an action, you need to ask yourself if there is a better way to do it. Only in this way, can you make progress in mastering Vim. For example, when your want to delete five lines of texts, if you press dd five times, you are certainly doing it the wrong way. After looking up the Vim manual, you know that you can use 5dd or d4j: you only need three keystrokes instead of ten!

The other day, I saw a post in a forum asking for help on how to quickly move the cursor inside a buffer. After using Neovim for nearly a year, I would like to share what I have learnt about fast movement and navigation inside Neovim/Vim.

There are hundreds or thousands of articles about basic Vim command to move the cursor. You can check this cheatsheet to quickly get an overview.

Here I want to to talk something more advanced, at least in my opinion. Unless otherwise notified, all the commands below are performed in normal mode.

Quickly go to a line

One common behaviour of Vim beginners is that they often do not know or forget that most Vim commands can be prefixed with a number to avoid manual repeat.

In this part, I talk about how to quickly go to line. Beginners often press j or k non-stop in order to navigate to a specific line below or above the current cursor line. This is a terrible and highly inefficient way to go to where you want.

To go to a line, first press the line number, then press gg or G. For example, to go to line 87, you press either 87gg or 87G.

Relative number

You can also move the cursor relatively based on the distance of a line to the current line. To go to ten lines below or above, you can press 10j or 10k.

This requires less key strokes once a file reaches hundreds of lines. But the only inconvenience is that you have to manually calculate the distance between the destination line and cursor line. In order to be more efficient, I suggest you turning on both the number and relativenumber options:

set number
set relativenumber

After setting these two options, the cursor line will show its absolute line number, while the other lines will show the relative distance to the current line. No more calculation needed!

For more settings about line number in Neovim, see my other post.

Precise cursor movement

Vim provides the f, t or F, T command to jump forward or backward to a character in current line, and you can repeat the action by pressing , or ; (see :h , or :h ;). While these commands are useful, they are not powerful enough to be of real value:

  • The movement is restricted to current line.
  • There are possibly many repetitions since you can only provide one character.

vim-sneak1 solves these two issues neatly. With vim-sneak, you can go anywhere your eyes can see in just four key strokes in most cases, which is one of the most amazing plugins I have ever seen.

After installing this plugin, add the following minimal settings to your configuration:

let g:sneak#label = 1
nmap f <Plug>Sneak_s
nmap F <Plug>Sneak_S

The above settings will map f and F to search forward and backward respectively and use the sneak label mode.

For example, if you want to go to the character i which is followed by m, first press f, then press im, all occurrences of im will be highlighted and given a label (see image below).

Press the character in a label, the cursor will jump to that location, which is really amazing.

Quickly go to a previous edit position

When I am writing Python source files, a pattern I notice is that I often forget to import a package or module when I need to use it. My usual work flow will be like:

  • Change to normal mode with ESC.
  • Go to the front of the source file.
  • Write the import statement I need.
  • Make other necessary adjusts.
  • Scroll down the source file and find the line I have been editing previously.
  • Go to the precise location where I am editing.

The annoyance is that it will take some time to go to the exact position we were editing before. This is when marks come to help2. A mark denotes a particular position in the buffer. The following is a simple overview to use marks:

  • ma: create a mark named a (can be a character in a-z)
  • `a: go the exact position denoted by mark a
  • 'a: go to the exact column denoted by mark a

With marks, you can simplify your work flow. If you want to move to other places but want to go back later, you can simply create a mark in this position. After finishing your edit in other places, you can directly go to the previous location with its mark label.

The native functionality provided by marks are powerful but not very convenient:

  • If you create several marks, you may not be able to remember the correspondence between buffer positions and mark labels.
  • You have to manually select an unused label for newly created marks. It is also cumbersome to delete a mark.

I recommend using the plugin vim-signature to manage your marks. This plugin will show the mark labels in the sign column3 so that you can easily go to a marked position. It also provides other commands to manage your marks.

Switching buffers quickly

When you are working on a large projects, you may open several files at the same time. You can go to a buffer quickly if you know its buffer index. To show the buffer index of opened files, use :ls. An example output is:

2 %a   "movement_navigation_inside_nvim.md" line 153
5 #    "~/.config/nvim/init.vim"      line 758

The number in the first column is the buffer index. To go to a buffer, first press the buffer index, then press Ctrl + 6.

It works but are still not really convenient, because you have to first find the buffer index using :ls command. My solution is to use vim-airline, which can be configured to show the buffer index on the tabline along with the file names. After installing vim-airline, add the following settings to your configuration:

let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#buffer_nr_show = 1

Restart Nvim and you will find that each buffer has a number on the left of the file name (see image below).

Conclusion

In this post, I introduced several ways for faster movement and navigation inside Neovim/Vim. I hope that you can apply the techniques here to improve your efficiency and save your time. Hopefully, you may share your own ideas on how to further improve efficiency.


  1. There is another similar plugin called vim-easymotion.
  2. see :h mark-motions for the documentation on marks.
  3. See :h signs for the documentation on signs.