V
V
Valentine2017-09-19 11:00:02
linux
Valentine, 2017-09-19 11:00:02

How to quickly switch buffers in VIM?

Regular VIM users, come on, tell us how you set up VIM for yourself to quickly switch buffers and what plugins you use. I am using the minibufexplorer plugin and the command mappings below. Does anyone have better options?

set hidden
nnoremap <Leader>l :ls<CR>
nnoremap <Leader>p :bp<CR>
nnoremap <Leader>n :bn<CR>
nnoremap <Leader>g :e#<CR>
nnoremap <Leader>1 :1b<CR>
nnoremap <Leader>2 :2b<CR>
nnoremap <Leader>3 :3b<CR>
nnoremap <Leader>4 :4b<CR>
nnoremap <Leader>5 :5b<CR>
nnoremap <Leader>6 :6b<CR>
nnoremap <Leader>7 :7b<CR>
nnoremap <Leader>8 :8b<CR>
nnoremap <Leader>9 :9b<CR>
nnoremap <Leader>0 :10b<CR>
nnoremap <Leader>b :ls<CR>:b<Space>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry, 2017-09-19
@dmtrrr

fzf

S
SilentFl, 2017-09-19
@SilentFl

if specifically for buffers, then I use them only in the variant :b part_of_filename
and if for navigation in general, then I have several tabs open (gt), windows are divided (fortunately, the monitor is large), navigation through files and classes (gf), plus I mainly work with rails - I use rails.vim, which gives transitions between controllers/views/models like :Eview, :Econtroller and so on. well, nerdtree for fs navigation

A
Alexey Pateleev, 2017-10-01
@031071

:ls - view all buffers;
:ls :b1 (for example) - see all open, go immediately;
:bf - b - buffer, f - first;
:bl - - , - ;
:b1 - if the required number is known;
:ls :b name file;

N
nrgian, 2019-05-08
@nrgian

https://habr.com/ru/company/mailru/blog/340740/

FZF comes with a basic Vim plugin, but its functionality is minimal, so FZF.vim is designed to provide all the features you need. The most useful commands are :Buffers, :Files and :Tags, I've mapped them to ;, ,t and ,r respectively:
nmap ; :Buffers
nmap t :Files
nmap r :Tags
The ; binding is important to me because I live in buffers. I don't use tabs much - more on that below - so it's important to me that I can switch to whatever I'm thinking about with as little effort as possible.

I'm a big believer in using buffers. I tried to work with tabs, but did not find any use in them. Tabs are an additional way to hide information, and in order to navigate to them, you need to memorize additional keyboard shortcuts or commands. If you have tmux, it's easier to open it in another Vim panel. And if you make good use of buffers, then you can easily get the desired file in a few keystrokes - using FZF, as described above.
Buffers are easy to deal with: when you start Vim, any file you open or create becomes a named buffer. You can view them with the :buffers command and navigate to one of them with :buf , where is any part of the buffer filename. Or with the help of numbers that are displayed by the :buffers command.
If you start Vim from the command line with multiple files as arguments, then each file will already be open in the buffer. If you have installed vim-unimpaired, the [b and ]b bindings will help you navigate easily between buffers.
I significantly accelerated this process by binding the ; FZF-command :Buffers, so that at one click of the button I get a list of buffers with a fuzzy search function. For example, if I opened the three files vim foo.txt bar.txt quux.txt on the command line, all I had to do was type ;qEnter to get to quux.txt. (Yes, it's similar to using :buf, but FZF shows a live preview when you have many similarly named files open.)
Sometimes I accidentally create buffers, like when I try to open a file, I type :e and press Enter too fast. The :bd command can be used to erase a buffer and remove it from the list, but that will also close the Vim window or split that has that buffer open. A good solution is bufkill.vim, which provides :BD to erase the current buffer and keep the current window open. I use it often, so I tied it to Meta-w.
If you need to rename, chmod, or delete a file, you can go to the terminal and make the change, but then Vim's buffer will become out of sync and display the annoying "File is no longer available" warning. It's better to take a NERDTree and highlight the current file with :NERDTreeFind, pressing m to change and choosing an action like move or rename. I prefer vim-eunuch, which adds a series of commands: :Chmod applies chmod to the current file, :Rename renames a file in its parent directory, :Move can move a file to another location, and :Delete will delete the file and buffer. There are a few more commands, but these are the ones I use most often.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question