2

I'm using vim and i want to switch between buffers by number. (e.g when i press Ctrl+2 vim should go to second buffer)

What should i write in .vimrc?

  • 2
    You can natively use `:buffer` — either `:b2` or `:2b` will do what you want. – Amadan Sep 17 '19 at 08:31
  • Yes, it works, thank you. what i have to write in ```.vimrc``` to create ```Ctrl+``` shortcut for it? – Kourosh Alinaghi Sep 17 '19 at 08:35
  • 2
    I don't think `Ctrl+` is mappable. See `:help keycodes` – Amadan Sep 17 '19 at 08:39
  • This doesn't directly answer your question, but the ctrlp plugin lets you do a fuzzy search through all your open buffers (or all the files in the directory etc etc) and has replaced the way I used to switch between buffers. – Stun Brick Sep 18 '19 at 14:49

3 Answers3

2

I personally have something like this in my .vimrc:

map <F9> :bp<CR>
map <F10> :bn<CR>

As stated in the comments and :help keycodes there is no available mapping for the numbers 0-9.

I tried using map <C-k1> ... (as those are available in the keycodes), but it didn't work.

GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40
wfehr
  • 2,185
  • 10
  • 21
2

I would suggest:

" list buffers and jump to a chosen one
nnoremap <Leader>b :ls<CR>:b<Space>

The command :ls<CR> will list all opened buffers and :b[uffer]<space> allows you to type the number shown by the ls

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
1

The answer depends on the approach of working with buffers.

I use the following three ways:

  • the buffers are showed in tabs;
  • the buffers are showed in windows;
  • the buffers can be hidden and the user switches between them using the current window.

For the first case, to switch between tabs, you may just type the buffer number and then hit the g and t keys. If it’s not suitable, make a map for the gt combination.

nnoremap <your_key_for_tab_switching> gt

For myself, I mapped the capital letters of the middle row, from “A” to “L”, for switching between tabs, but it may be not suitable for every user:

nnoremap A 1gt
nnoremap S 2gt
nnoremap D 3gt
...
nnoremap L 9gt

For switching between windows I prefer just the “next” and “prev” commands:

nnoremap <your_keys_to_the_next> <c-w>w
nnoremap <your_keys_to_the_prev> <c-w>W

To switch between opened but currently hidden buffers within a window you can:

nnoremap <your_shortcut_key> :buffer 
" there is a space after “buffer”

That will be exactly you’ve asked for — “by numbers”. But it’s not a convenient way as I think. I prefer to switch between hidden buffers by short names but it’s not a standard feature.

There is yet another way — capital letters and digits for the marks. See help ma for more information.

And a little piece of advice: consider using letter-like keyboard keys instead of the number keys because they are placed much more closely to the fingers.

oneastok
  • 323
  • 1
  • 11
  • 1
    There are many useful uppercase mappings on the home row. I would not suggest overriding them. Using `:b {partial-name}` is a standard feature. Can improve your mapping some: `nnoremap {key} :ls:b`. May also want to looking into [using buffers effectively](https://stackoverflow.com/a/21338192/438329) – Peter Rincker Sep 18 '19 at 15:07
  • Yes, you’re completely right! I remapped a lot of standard keys for myself but it’s not suitable for another user. But the mappings I’ve written are just samples reflecting the possible ways of using buffers. Everyone has his own habits and vim mappings. I just wanted that the topic starter would feel free to map his key combinations. The article you referred is a very good one, I believe the topic starter should read it. – oneastok Sep 18 '19 at 18:15