3

from neovim help:

bufnr([{expr} [, {create}]])
        The result is the number of a buffer, as it is displayed by
        the ":ls" command.  For the use of {expr}, see |bufname()|

But it is not true.

the result of :ls

  1  a   "trackforlam.py"               line 73
  6  a   "[jupyter]"                    line 233
  9  h + "[No Name]"                    line 522
 24  h   "[jupyter]"                    line 0
 30 #h   "~/vimpy3/mappings.vim"        line 593
 31 %a   "~/vimpy3/hacks.vim"           line 506

echo bufnr('[juypter]')

returns 1

Why is that? is it the window number? So how do I get the buffer number ?

2
  • Can you confirm 1 is returned, or does it return -1? – Quasímodo Aug 28 '20 at 23:01
  • 1 is returned. Someone answered correctly but didn't provide a proper fix. The thing is that it happens because '[juyper]' is like regex and matches either j or u or ... . The question is how to make '[' be char in a file pattern? – eyal karni Aug 29 '20 at 0:05
1

From :h file-pattern:

[ch] matches 'c' or 'h'

Using brackets like in [jupyter] matches a single character present in the list jupyter, not the literal string '[jupyter]'. You need to escape your square brackets inside your pattern like:

bufnr('\[jupyter\]')

Otherwise, [jupyter] is being treated as a collection, and the t inside that collection is matching trackforlam.py, which is why you are getting 1 as a result.

See:

:h file-pattern
:h bufnr()
:h bufname()
:h /magic
:h literal-string
4
  • I tried it before and I was sure it didn't work. echo bufnr("\[jupyter\]") in my history! maybe because there were duplicate buffers? works now – eyal karni Aug 29 '20 at 12:08
  • If you use double quotes you have to double escape (i.e. echo bufnr(“\\[jupyter\\]”)) as outlined in :help literal-string. Because we want a literal square bracket in the pattern, not the string, the backslash has to be escaped. – Jake Grossman Aug 29 '20 at 14:41
  • Does this work with a new, [No Name] buffer for you? Not for me :( – Quasímodo Aug 30 '20 at 15:33
  • @Quasímodo In :h bufnr() it says to reference :h bufname() for the use of the {expr} argument. Because calling bufname() on a new, [No Name] buffer returns the empty string, I wouldn't expect to be able to search for it. That was my take, at least. What you are searching for is a pattern in the name, after all. – Jake Grossman Aug 30 '20 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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