Mastodon hachyterm.io

I emphasize using buffers over tabs in VIM.

Buffers are the ideomatic way of using VIM. You can read more about that here: Vim Tab Madness. Buffers vs Tabs.

I customized my settings with some features, which I copied from Josh Davis’s post above:

" This allows buffers to be hidden if you've modified a buffer.
" This is almost a must if you wish to use buffers in this way.
set hidden

" Close the current buffer and move to the previous one
" This replicates the idea of closing a tab
nnoremap <leader>bq :<c-u>bp <bar> bd #<cr>

" Show all open buffers and their status
nnoremap <leader>bl :ls<cr>

I also use Tim Pope’s vim-unimpaired. You can use [b and ]b to jump to the previous or next buffer.

To open up a new buffer, I use :e and append the file path, for example: :e src/components/header/Header.js.

After a while, I have a lot of open buffers. I can close them manually with <leader>bq (as per custom key binding).

But what if I want to close all buffers except the current one?

There are two solutions:

  1. Create a custom command
command Bd :up | %bd | e#

This updates the current buffer (saves changes), then closes all open buffers, and reopens the last buffer.

Use the command by typing :Bd into the VIM console.

  1. Make a custom key binding

I prefer to have a custom key binding. <leader>bq already closes the current buffer (see above), so <leader> bd should close all buffers except the current one.

nnoremap <leader>bd :<c-u>up <bar> %bd <bar> e#<cr>

This runs the same command as above but as a key binding.

Further Reading