How to quickly move to a specific buffer?

36 views
Skip to first unread message

Steve Litt

unread,
Dec 13, 2022, 8:10:00 PM12/13/22
to v...@vim.org
Hi all,

I typically use :bn and :bp to move between buffers, but I have five
buffers open on my current project, which requires a heck of a lot of
keystrokes to move to a specific buffer. So I'd like a command to bring
up a list of buffers so I can chose a specific one.

I've looked up :h buffers and can't find anything. Is there a way I
could do this?

Theoretically I could use the gvim menu system to do this, but it
involves taking my hands off home position to use the arrow keys, and
also, Alt+b is a difficult hotkey, so I'd much rather have some sort of
Vim command to do it.

Thanks,

SteveT

Steve Litt
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm

Tim Chase

unread,
Dec 13, 2022, 8:21:57 PM12/13/22
to vim...@googlegroups.com
On 2022-12-13 20:09, Steve Litt wrote:
> I typically use :bn and :bp to move between buffers, but I have five
> buffers open on my current project, which requires a heck of a lot of
> keystrokes to move to a specific buffer. So I'd like a command to bring
> up a list of buffers so I can chose a specific one.

I usually use

:ls

though there's also similarly

:files
:buffers

with details at

:help :files

to list the buffers I currently have open, and then use "#" followed
by the buffer-number to edit that particular buffer-number or split
it in a new window:

:e #38
:sp #38

as detailed at

:help :_#n

If I have two that I'm flipping back and forth between, I'll use
control+^ (:help CTRL+^) to toggle between the current file and the
alternate file.

:help alternate-file

Hopefully that gives you some reasonable built-in solutions. I'm
sure there are all sorts of file-navigation plugins, but the stock
functionality works fine for me.

-tim




Lang Hurst

unread,
Dec 13, 2022, 8:28:45 PM12/13/22
to vim...@googlegroups.com, v...@vim.org
I copied this off the vim website, I think. In my .vimrc

nnoremap <F5> :buffers<CR>:buffer<Space>

Hit F5, just enter the number of the listed buffers.
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20221213200942.40af4307%40mydesk.domain.cxm.

--
“To argue with a man who has renounced the use and authority of reason, and whose philosophy consists in holding humanity in contempt, is like administering medicine to the dead, or endeavoring to convert an atheist by scripture.” ― Thomas Paine, The American Crisis.

John Passaro

unread,
Dec 13, 2022, 8:30:12 PM12/13/22
to vim...@googlegroups.com
good stuff. also, `:b` completion shows your open buffers. and ^6 is not only good for going between two, not if you have three and can remember their buffer numbers via :ls, <n>^6 takes you to buffer number n.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

Arun

unread,
Dec 13, 2022, 9:23:02 PM12/13/22
to vim...@googlegroups.com, v...@vim.org
Usually, I :split open (maximized) my active files and switch back and forth using some helpful mappings (Alt-j and Alt-k). When the number of splits are more, I open a tab and do similar stuff there. I usually do not like the ":split" opening window unmaximized, so these mappings help in achieving that:

"--8<--
set nocp
set winminheight=0
set laststatus=2

au WinEnter * wincmd _

"Map Alt-j to edit split window one below. "Alt" works well in GUI, use something else inside terminals
nnoremap <m-j> <c-w>j
"Map Alt-k to edit split window one above
nnoremap <m-k> <c-w>k
"--8<--

Regards,
-Arun

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

Tim Chase

unread,
Dec 13, 2022, 9:46:14 PM12/13/22
to vim...@googlegroups.com
On 2022-12-13 20:29, John Passaro wrote:
> and ^6 is not only good for going between two, not if you have
> three and can remember their buffer numbers via :ls, <n>^6 takes
> you to buffer number n.

You've pointed out the critical failure here...my inability to
remember more than one (alternate) buffer-number. ;-)

-tim




Yegappan Lakshmanan

unread,
Dec 13, 2022, 9:56:22 PM12/13/22
to vim...@googlegroups.com

Starting with Vim9, fuzzy completion of buffer names is supported.

- Yegappan

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

Steve Litt

unread,
Dec 13, 2022, 10:22:51 PM12/13/22
to vim...@googlegroups.com
Arun said on Tue, 13 Dec 2022 18:22:41 -0800

>Usually, I :split open (maximized) my active files

For me, :split only makes 2 windows, not a window for every buffer.

> and switch back and
>forth using some helpful mappings (Alt-j and Alt-k). When the number of
>splits are more, I open a tab

I looked up :h tab and found stuff seemingly unrelated. How does one
open a tab?

Steve Litt

unread,
Dec 13, 2022, 10:25:05 PM12/13/22
to vim...@googlegroups.com
Yegappan Lakshmanan said on Wed, 14 Dec 2022 08:26:03 +0530

>Starting with Vim9, fuzzy completion of buffer names is supported.
>
>- Yegappan

I did :h fuzzy but it said nothing about fuzzy completion. How do I
achieve fuzzy completion, and how much of the filename (or something
else) do I have to type in?

Steve Litt

unread,
Dec 13, 2022, 10:29:24 PM12/13/22
to vim...@googlegroups.com
Lang Hurst said on Tue, 13 Dec 2022 17:28:39 -0800

>I copied this off the vim website, I think. In my .vimrc
>
> nnoremap <F5> :buffers<CR>:buffer<Space>
>
>Hit F5, just enter the number of the listed buffers.

This is almost exactly what I need, the two exceptions being:

1) It becomes very time consuming if the number of buffers exceeds the
height of the Vim window. However, this will usually not be the case.

2) F5 is a very inconvenient hotkey. Is there a way I could use
Shift+Ctrl+n ? How would I change the <F5> to change it to
Shift+Ctrl+n ?

Steve Litt

unread,
Dec 13, 2022, 10:49:39 PM12/13/22
to vim...@googlegroups.com
Steve Litt said on Tue, 13 Dec 2022 22:29:15 -0500


>2) F5 is a very inconvenient hotkey. Is there a way I could use
> Shift+Ctrl+n ? How would I change the <F5> to change it to
> Shift+Ctrl+n ?

I figured it out. Shift+Ctrl+n is <s-c-n>. Two consecutive pushes of
Alt+n is <a-n><a-n>. Both of those are better for me than F5, and both
are sufficient for my needs.

So this is the solution I'm going to use.

Enan Ajmain

unread,
Dec 14, 2022, 12:39:48 AM12/14/22
to vim...@googlegroups.com
On Wed, 14 Dec 2022 08:26:03 +0530
Yegappan Lakshmanan <yega...@gmail.com> wrote:
> Starting with Vim9, fuzzy completion of buffer names is supported.

By fuzzy completion, do you mean ':b cdent' should expand to a buffer
'src/cindent.c'? or do you mean I need to use ':b c*dent'? Because the
latter is slightly different, I wouldn't necessarily call that fuzzy.

--
Enan
3nan....@gmail.com
https://git.sr.ht/~enan/
https://www.github.com/3N4N

Enan Ajmain

unread,
Dec 14, 2022, 1:26:41 AM12/14/22
to vim...@googlegroups.com
On Tue, 13 Dec 2022 22:22:41 -0500
Steve Litt <sl...@troubleshooters.com> wrote:
> For me, :split only makes 2 windows, not a window for every buffer.

I don't think you wanted this, because I have never used it, but if you
do want to open windows for each buffer, you can use ':h bufdo'.

:bufdo split
:bufdo vsplit

Arun

unread,
Dec 14, 2022, 3:09:05 AM12/14/22
to vim...@googlegroups.com
On Tue, Dec 13, 2022 at 7:22 PM Steve Litt <sl...@troubleshooters.com> wrote:
Arun said on Tue, 13 Dec 2022 18:22:41 -0800

>Usually, I :split open (maximized) my active files

For me, :split only makes 2 windows, not a window for every buffer.

# vim -o <file1> <file2> ..

If you are already inside vim, you can either manually run ":split <file1>", ":split <file2>" .. OR
    :bufdo split (Close those you do not want)

One nice tip here is that, once you place your buffers in vim to your taste, you can run the command:
    :mksession <session-file>
..to save those windows placement. Later run
    :source <session-file>
..to restore the view. That is a huge time saver sometimes.


> and switch back and
>forth using some helpful mappings (Alt-j and Alt-k). When the number of
>splits are more, I open a tab

I looked up :h tab and found stuff seemingly unrelated. How does one
open a tab?

Either use:
    $ vim -p <file1> <file2> ..
..or inside vime:
    :tabe <file1>

:help tabedit

Regards,
-Arun 

Thanks,

SteveT

Steve Litt
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

North Year

unread,
Dec 14, 2022, 3:28:40 AM12/14/22
to vim...@googlegroups.com
On 12/13/22 20:09, Steve Litt wrote:
>Hi all,
>
>I typically use :bn and :bp to move between buffers, but I have five
>buffers open on my current project, which requires a heck of a lot of
>keystrokes to move to a specific buffer. So I'd like a command to bring
>up a list of buffers so I can chose a specific one.
>
>I've looked up :h buffers and can't find anything. Is there a way I
>could do this?
>
>Theoretically I could use the gvim menu system to do this, but it
>involves taking my hands off home position to use the arrow keys, and
>also, Alt+b is a difficult hotkey, so I'd much rather have some sort of
>Vim command to do it.

Do you want to do it in a barebone vim (i.e) no plugin way, or you are
opt to use plugin to do that?

Might be a bit offtopic here, I use neovim, so to find a buffer I use
"Telescope buffers". As what I remembered, LeaderF, Coc.nvim, and
fzf.vim might be the alternatives in vim.

And if you want to do it in the cmdline, then ":ls" to show all buffers
and then":b xxx" might be the only viable solution. The default vim's
completion system works not that good, it is neither case-insensitive
nor fuzzable (it works similar to the default bash?). I used nvim-cmp as
my auto-completion framework which also works in cmdline, so when I type
":b xxx" an auto-completion popup invokes and I can just complete it
with fuzzy matching. What first comes in my mind of the alternative of
nvim-cmp in vim is coc.nvim, but there are definetely other
alternatives.

Yegappan Lakshmanan

unread,
Dec 14, 2022, 8:53:26 AM12/14/22
to vim...@googlegroups.com
On Wed, Dec 14, 2022 at 8:55 AM Steve Litt <sl...@troubleshooters.com> wrote:
Yegappan Lakshmanan said on Wed, 14 Dec 2022 08:26:03 +0530

>Starting with Vim9, fuzzy completion of buffer names is supported.
>
>- Yegappan

I did :h fuzzy but it said nothing about fuzzy completion. How do I
achieve fuzzy completion, and how much of the filename (or something
else) do I have to type in?

You need to add ‘fuzzy’ to ‘wildoptions’.  This enables fuzzy completion for command line completion.

- Yegappan

Yegappan Lakshmanan

unread,
Dec 14, 2022, 8:55:39 AM12/14/22
to vim...@googlegroups.com
On Wed, Dec 14, 2022 at 11:09 AM Enan Ajmain <3nan....@gmail.com> wrote:
On Wed, 14 Dec 2022 08:26:03 +0530
Yegappan Lakshmanan <yega...@gmail.com> wrote:
> Starting with Vim9, fuzzy completion of buffer names is supported.

By fuzzy completion, do you mean ':b cdent' should expand to a buffer
'src/cindent.c'? or do you mean I need to use ':b c*dent'?  Because the
latter is slightly different, I wouldn't necessarily call that fuzzy.

I am referring to the former.  You can enable this by adding ‘fuzzy’ to ‘wildoptions’.

- Yegappan

Owajigbanam Ogbuluijah

unread,
Dec 14, 2022, 12:50:48 PM12/14/22
to vim...@googlegroups.com
I have this in my setup

set wildmenu
set wildoptions=pum,fuzzy


With this, the command fuzzy-completes in a pop-up window.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

IFo Hancroft

unread,
Dec 15, 2022, 1:00:14 AM12/15/22
to vim...@googlegroups.com
Since no one mentioned this:

You can use ':b<buffer number>' to move to a specific buffer.
When I forget which buffer is which number, I execute ':ls' and then I
do ':b2' for buffer number 2, ':b 1' for the first buffer, etc

Enan Ajmain

unread,
Dec 15, 2022, 3:04:33 AM12/15/22
to vim...@googlegroups.com
On Wed, 14 Dec 2022 19:25:19 +0530
Yegappan Lakshmanan <yega...@gmail.com> wrote:
> I am referring to the former. You can enable this by adding ‘fuzzy’
> to ‘wildoptions’.

Damn! Didn't even know about it. Thanks.

BPJ

unread,
Dec 22, 2022, 5:38:40 AM12/22/22
to vim_use, v...@vim.org
I usually have one tab open for each buffer. Then I can jump between the tabs/buffers with COUNT + gt. You can set up 'tabline' to display the tab number and/or use :tabs (which I have mapped to <F-11>t) to see a list of the tabs. (Note: when I'm on termux and don't have too many tabs open I can just point my thumb on the tagline to switch — purists may shudder all they want! :-)

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

Enan Ajmain

unread,
Dec 22, 2022, 7:08:17 AM12/22/22
to vim...@googlegroups.com
On Thu, 22 Dec 2022 11:38:28 +0100
BPJ <b...@melroch.se> wrote:
> I usually have one tab open for each buffer. Then I can jump between
> the tabs/buffers with COUNT + gt. You can set up 'tabline' to display
> the tab number and/or use :tabs (which I have mapped to <F-11>t) to
> see a list of the tabs. (Note: when I'm on termux and don't have too
> many tabs open I can just point my thumb on the tagline to switch —
> purists may shudder all they want! :-)

I did shudder, but I'm not gonna begrudge you your way. As long as it
works, no need to fix it. I just wanna add that using tabs as buffers
will keep you from using tabs as window-manager. I use tabs that way:
each tab has a set of windows, split and resized just the way I want.
The tabs are separated a/c a context. The context maybe a project (so
one tab per project) or a module (one tab for 'src/handle/', another for
'src/menu/').

Use cases:

o If I want to open a file without destroying the perfect arrangement of
windows I have got going on, I can open the file in a new tab.
o If I want to work on a different module, I can:
o Open a new tab
o ':tcd src/module/'
o ':e **/*<tab>'
That way I have a new workspace for that module.

If you don't require this workflow, or you've got a substitute for it,
using tabs as buffers won't be any less pure. If not, maybe you can do
a intuitive cost-benefit analysis and see if adopting a different
approach (buffers as buffers and tabs as window-manager) seems better.
Reply all
Reply to author
Forward
0 new messages