When we are using Neovim, we often open several files in a window. Each file correponds to a buffer. How to switch to a buffer quickly?

The native way

In command mode, some of the commands to change buffers are list below:

  • :bfirst: change to first buffer
  • :blast: change to last buffer
  • :bnext: change to next buffer
  • :bprevious: change to previous buffer

Or you can use :b <TAB> to choose from currently opened buffers.

If there are a few buffers, it is easy to switch using the above command. But if we have a dozen of opened buffers, it is cumbersome to use these commands.

Switching buffers using buffer number

Fortunately, we can switch to a buffer quickly if we know its number. To get a buffer’s number, use :buffers command in normal mode. It will print something like:

3      "train.py"                     line 1
4 %a   "main.py"                      line 17
6      "keys.py"                      line 1
7      "utils.py"                     line 1
8 #    "val_CTC.py"                   line 2

The first number is the number corresponding to each buffer. Then we can use NUM Ctrl-6 to go to a particular buffer (replace NUM with actual buffer number). But this is still inconvenient — since we have to find the buffer number first.

A better solution for buffer switching

If you have installed vim-airline,you can configure it to show the buffer number on the top of the window tabline. Just add the following settings to Nvim config file inti.vim:

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. Then you can easily go to a buffer.