Quickly switching buffers in Vim normal mode
Asked Answered
R

15

94

Recently I found out that I'm "using tabs incorrectly" in Vim. I've been trying to just use buffers in Vim since, assisted through MiniBufExplorer, but I find it painful because of how many keystrokes it takes to change buffers from normal mode. With tabs, I can just do gt or gT to hop back and forth between tabs in normal mode, and I can also do NUMBERgt to go to a specific tab.

With buffers, I either have to enter command mode with :bn, :bp, or with MiniBufExplorer, use Ctrl + k or Ctrl + Up to hop up to the buffer window, scroll left or right with h and l and then hit Enter to select the buffer I want. Or I can do something involving a leader sequence, but it always requires removing multiple fingers away from home row. That's a real pain.

How can I get something equivalent switching tabs in normal mode to switch buffers in normal mode, so I can do something like gn/gp for :bn/:bp and NUMBERgn for :buf NUMBER?

Raeannraeburn answered 5/4, 2011 at 21:55 Comment(0)
S
87

Add this to your .vimrc

map gn :bnext<cr>
map gp :bprevious<cr>
map gd :bdelete<cr>  

" I personally use <leader> 
map <leader>n :bnext<cr>
map <leader>p :bprevious<cr>
map <leader>d :bdelete<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.

Scarfskin answered 6/4, 2011 at 7:13 Comment(3)
Good suggestion on using <leader>. Originally I didn't like this because \ is the original leader and was not easy to reach, but of course, the right thing to do was redefine the leader to be something close to home-row, in my case ,, which is easier than ; for Dvorak layout.Raeannraeburn
this would possibly affect gn (select next instance of last search), which some might use with some macros and repeats.Idolum
Having <leader>d as a mapping to :bd I find to be more annoying than worth it. It's quite easy to type <leader>d by accident and then you have to go and re-open your file again. I find this risk more annoying than typing :bd each time. But then typically I almost never delete the buffer, I just leave all the buffers open/hidden and then run :%bd to delete them all once I'm done on a certain piece of work. It's one of the nice things of Vim that you don't really have to care about how many buffers you have open.Holism
C
61

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.

Cleptomania answered 6/4, 2011 at 18:27 Comment(2)
You don't even have to start typing any part of the file, just type :b <Tab> (N.B. don't forget to add a space) and it will start cycling though the buffers you have open. Helpful if you can't even remember the filenamesHolism
I find including lastused in my wildmode (I use set wildmenu wildmode=lastused:full) is very convenient as it cycles through matches in most recently used order, making it easy to jump back to a recent buffer.Webber
M
43

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.

Morgen answered 6/4, 2011 at 7:56 Comment(6)
Very nice. Easily the best solution. tab is after all a navigation key. how about ctl-sh-tab and ctl-tab for tabs?Coachman
@HörmannHH Unfortunately, some shortcuts cannot be mapped in Vim, which is the case of <C-Tab>.Duluth
this is great. I'm stealing itMosemoseley
If you have :set hidden do you need the values of the modifiable, readonly, modified?Apthorp
Fantastic answer.Oddball
ancient answer by now, but small request - maybe better to quickly explain the benefit of adding the modifiable, readonly and modified terms instead of a simple map/remap/inoremap.Endorse
S
41

{count}CTRL-^ switches to the count numbered buffer.

Simonette answered 6/4, 2011 at 7:32 Comment(2)
So remember every buffer ID? do :buffers before this or see my answer...Jahdal
Use the MiniBufExplorer plugin to see the numbers and this to switch between themLennielenno
H
28

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:

  1. 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.

  2. 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

Holism answered 13/4, 2017 at 18:32 Comment(0)
A
18

I use the plugin unimpaired.vim

it defines mappings [b and ]b that jump to the previous and next buffer in the list.

For jumping for a specific buffer the best option I know is the one you mentioned: :b<number>

If you go into another buffer you can came back quickly by typing <c-^>

Allelomorph answered 5/4, 2011 at 22:10 Comment(0)
O
8

Expanding on Rumple Stiltskin's answer, if you know that the file you want to get to is in buffer 4, for example, you can get there quickly with

4Ctrl-^

On my UK keyboard, I can actually do 4Ctrl-6, as explained in

:help CTRL-^

By the way, you can see the buffer numbers with

:buffers

or

:ls
Ornas answered 26/5, 2015 at 5:48 Comment(0)
M
7

I use LustyExplorer: I hit <leader>b to open a list of buffers then a couple of letters from the name of the buffer I want to open then enter. Easy.

But, you are not "using tabs incorrectly", you are using tabs the way you want. If it worked for you why go through the pain of unlearning your way to learn "the right way"?

Maid answered 6/4, 2011 at 7:15 Comment(1)
LustyExplorer looks very neat and may be what I finally settle on. There's a very good video demo at lococast.net/archives/185 around 7 minutes. Thanks.Raeannraeburn
G
7

I use F9 and F10 to move between the previous/next buffer with this mapping:

map <F9> :bprevious<CR>
map <F10> :bnext<CR>

For me this is the fastest way to switch buffers.

Gifford answered 29/8, 2016 at 8:27 Comment(0)
Z
4

fzf.vim is another fast way to changes buffers using fuzzy matching. This plug-in ships with the default command:

:Buffers

which opens the list of all open buffers similar to :ls but a buffer can be (fuzzy) searched and selected. Opening the buffer in the current window is through enter, but can can also be opened in a new split (h or v) or tab using ^X ^V or ^T respectively.

Noteworthy is also:

:Lines

Which allows to search through the content of all open buffers. This can be handy if you forget the name of a buffer but you know what it should contain.

Zither answered 8/10, 2017 at 20:2 Comment(1)
FZF is generally great and I always use it when opening a file that isn't in the buffers. 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. This is normally enough for me. I also find that the :buffers command appears instantly where as there is a frustrating delay when opening :Buffers (at least in GVim on Windows which is where I'm using Vim most).Holism
J
3

I make it easier for myself: In .vimrc :

nnoremap <leader>bf :buffers<CR>:buffer "<- Last spaces is necessary

For example, in normal mode, say your leader key is \(default it is), type \bf, then you have a list of opened buffers, type number of buffer you want and hit enter key. NOTE: remember that last spaces not necessary at all if you wich type it after :D

Jut like that ;)

switching buffers in vim with above nnoremap

More detail:

<C-O> Navigate backward

<C-I> Navigate forward

So there is no need extra remapping, otherwise you remapped them.

Jahdal answered 22/7, 2020 at 20:40 Comment(0)
S
3

Here is my solution:

" `<leader><Tab>` - next buffer;
nnoremap  <silent> <leader><Tab>    :bnext<CR> 

" `<leader><S-Tab>` - previous buffer;
nnoremap  <silent> <leader><S-Tab>  :bprevious<CR>

" `_bufferNumber_ + <Tab>` - go exact the buffer number;
nnoremap  <silent> <Tab>            <C-^>

By the way, I use 'buftabline' plugin and set let g:buftabline_numbers = 1 to spread my buffer on the tabline.

buftabline

Suomi answered 20/12, 2020 at 4:57 Comment(0)
M
1

I think bufexplorer is a nice plugin to use. <leader>be brings up an interactive buffer explorer that lists all open buffers. You could quickly move through the list and Enter puts you in the selected buffer. Unlike LustyExplorer It has no dependency to ruby.

Militarism answered 10/10, 2017 at 22:6 Comment(0)
A
0

I prefer navigating between buffers similarly to how I'm navigating between window panes: <alt-h> and <alt-l>. This is to straightforward to set on Mac because <alt>/<option> key binds are bounded to specific characters.

" Buffer navigation
map ˙ :bp<cr>
map ¬ :bn<cr>
map § <c-^>

Here is a good answer that shows how you can see characters maped to <alt-..> combinations

Acrid answered 12/10, 2021 at 19:21 Comment(0)
W
0

To anybody using nvchad and wondering how to do this

map('n','<leader>bn','<cmd>bnext<CR>')
map('n','<leader>bp','<cmd>bprevious<CR>')
map('n','<leader>bd','<cmd>bdelete<CR>')

I am using this to change buffers

Whidah answered 9/5 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.