1

I've been searching far and wide around tutorials, :help command, source code, Stacks but maybe I just can't form my query correctly in the vimscript lingo.
My goal is to have a short command for switching buffers like:
42B or B42 that will switch the view to 42nd buffer. I don't want to monkey-map all the series B1, B2, B3 ..., though. So I want to treat 42 as an argument to some underlying function that chooses the appropriate buffer.

Pls halp.

1 Answer 1

1

There actually is a built-in map which does this, <c-^> or control+^

If you don't like this (it is indeed difficult to press), you can remap it simply.

nnoremap gb <c-^>

Note: I am using gb here since B is already a vim command. You can use B if you really want, but be aware that you would be overriding a built-in.

I am also going to show how to make this map if there weren't already a command <c-^>. Since there is an ex-command :buffer N which does what we need, the map would take the following pattern:

nnoremap <silent> gb :<c-u>buffer <c-r>=v:count<cr><cr>

The only tricky parts are <c-u> which eliminates the line-oriented address vim places after : by default and <c-r>=v:count<cr> which means enter the current count into the cmdline.

1
  • 1
    This is brilliant! Thank you so much. I'll definitely be using that. For the record, <c-^> chooses the buffer with that exact number and I wanted something more robust, that would be able to skip over the holes coming from closed buffers. But it's unnecessary complex and I can always just map :bn to something sensible... Commented Apr 11, 2020 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.