romgrk / barbar.nvim

The neovim tabline plugin.
2.2k stars 81 forks source link

How to configure BufferGoto properly #412

Closed bavo96 closed 1 year ago

bavo96 commented 1 year ago

I want to move around buffers using its index. Right now I have to setup keymaps as below image: image Is there any oneline code that helps me not to set the keymaps manually for buffer index ? (like moving from 1 to n buffer).

romgrk commented 1 year ago

Not sure I understand the question. Are you looking for a loop that sets BufferGoto instead of repeating the mapping 10 times?

bavo96 commented 1 year ago

Yeah I think I need a loop to set or maybe is there any way to help me do like vim.keymap.set('n', '<leader>num', '<Cmd>BufferGoto num<CR>', opts) according to user's number. Just like we hit 10G and it goes to line 10 :D In case I have to open like 20 files and I need to enter <Space>20 to go to file 20.

romgrk commented 1 year ago

I'd recommend using a for loop and setting the end index to the number of buffers you want to support. You can use string.format('<cmd>BufferGoto %i<CR>, i) to generate the mapping command.

Iron-E commented 1 year ago

Yeah I think I need a loop to set or maybe is there any way to help me do like vim.keymap.set('n', '<leader>num', '<Cmd>BufferGoto num<CR>', opts) according to user's number. Just like we hit 10G and it goes to line 10 :D In case I have to open like 20 files and I need to enter <Space>20 to go to file 20.

You can use vim.v.count instead of doing it manually. When doing 50gg, vim.v.count is 50. Something like:

vim.keymap.set('n', 'YOUR MAPPING', function() 
  vim.cmd.BufferGoto(vim.v.count) -- or `vim.api.nvim_command('BufferGoto ' .. vim.v.count)`
end)
bavo96 commented 1 year ago

I've tried both solutions and it seems like @Iron-E 'solution got a delay (about 2 secs) when switching to another buffer. @romgrk 's solution brings me to another buffer like immediately (don't know why). Besides, it makes me feel normal when I press <Space>number so i'll stick with the for loop. Thank you guys <3