Quickly switching buffers in Vim normal mode
Solution 1:
Add this to your .vimrc
map gn :bn<cr>
map gp :bp<cr>
map gd :bd<cr>
Note that you are remapping gp
and gd
, but maybe you don't care about that (:help gp
, :help gd
).
For more information on how to map key strokes see :help map-overview
and :help map.txt
.
Btw, I personally use <leader>
instead of g
in the mapping. My <leader>
is set to ;
. This puts my <leader>
key on the home row which makes me willing to map all kinds of stuff using <leader>
. :help mapleader
if you want to change your <leader>
key.
Solution 2:
The way I usually switch between buffers is to use the :buffer
command with the built-in autocompletion, e.g. :b prof<Tab>
to switch to folder/path/LoginProfileFactory.php
.
You can just start typing any part of the file name of the buffer you need, which is nice.
Less often, I actually remember the numbers of the buffers I want and I use something like :b 3
or :3b
.
I see you mention you don't like :buf 3
though, so Rumple Stiltskin has an alternative to the :3b
style that you may prefer.
Solution 3:
{count}CTRL-^
switches to the count
numbered buffer.
Solution 4:
I have the following lines in .vimrc:
nnoremap <silent> <tab> :if &modifiable && !&readonly && &modified <CR> :write<CR> :endif<CR>:bnext<CR>
nnoremap <silent> <s-tab> :if &modifiable && !&readonly && &modified <CR> :write<CR> :endif<CR>:bprevious<CR>
Now a Tab let you go to the next buffer and a Shift-Tab to the previous.
Solution 5:
This is based on Nick Knowlson's answer, but I wanted to expand on my comment there ...
Type
:b <Tab>
(note the space), then cycle through the open buffers with Tab or ← / →.
... which gets us closer to the Ctrl + Tab in all the other editors and browsers I use.
It's actually even better in some ways, you can then go backwards and forwards with ← / → arrows. It avoids the thumb + finger fu to type Ctrl + Shift + Tab to go backwards through the tabs in editors and browsers.
N.B. Shift + Tab just does the same as Tab
This is then actually something like Win + Tab in Windows 10, where once you first open up the window and you can then move around using the arrow keys.
Edit: I have two further tricks that I picked up for using buffers:
-
From this answer I have this in my
.vimrc
:nnoremap <leader>bb :buffers<cr>:b<space>
it opens the
:ls
/:buffers
command and pre-types the:b
so that you just have to type the buffer number as you'll see a list with all the buffers and their numbers. -
I also have
nnoremap <leader><tab> :b#<cr>
which toggles between the current and most recently used buffers, it's a bit like doing
cd -
when switching back and forth between directories