Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: difference between :n and :bn

Tags:

vim

If I started vim with multiple files like so vim *.java, I can cycle through opened files using either :n or :bn (and other related commands).

But if I start with only one file and load other files using :split (and later close the split window), I can cycle the buffers using :bn but not :n.

What is the difference between these two situations? If I do :buffers in both cases, there is no difference whatsoever in the buffer list. It might seem like I am asking unnecessary question but I would like to understand if there is any gotchas lurking under the hood. Thanks.

like image 865
hummingV Avatar asked Sep 01 '16 06:09

hummingV


2 Answers

TL;DR: :bn et al. always cycle through all buffers, :n et al depend on how the buffers were created.

Explanation:

:n is short for :next which moves in the argument list (which you can view by doing :args)

:bn is short for :bnext which moves in the buffer list

Opening a file with :sp foo does not alter the argument list, but adds a buffer, and hence does not change the behaviour of:n, but does affect :bn.

On the other hand, if you open a new file with :n foo, that replaces the argument list (also changing the behaviour of :n et al, but not that of :bn et al.) .

An example session:

$ vim /tmp/foo /tmp/bar

:args
[foo] bar

:buffers
  1 %a   "foo"                          line 1
  2      "bar"                          line 0

Here, the buffer and argument lists match

:sp /tmp/sna

:args
[foo] bar

:buffers
  1 #a   "foo"                          line 0
  2      "bar"                          line 0
  3 %a   "sna"                          line 1

Now, there is a new buffer, but the argument list is the same

:n /tmp/test /tmp/baz

:args
[test] baz

:buffers
  1  a   "foo"                          line 0 
  2      "bar"                          line 0 
  3 #    "sna"                          line 1 
  4 %a   "test"                         line 1 
  5      "baz"                          line 0

And now the argument list was replaced, and the buffer list extended.

like image 61
drRobertz Avatar answered Sep 30 '22 20:09

drRobertz


:bnext loads the next buffer from the buffer list in the current window.

:next loads the next file from the argument list in the current window.

In practice, every file in the argument list is loaded into a buffer and thus added to the buffer list but, while changes to the argument list may have some impact on the buffer list, changes to the buffer list never have any impact on the argument list. This means that the two lists can be equivalent but that there's no guarantee at all about that.

:n may load the next buffer from the buffer list and :bn may load the next file in the argument list but this shouldn't be considered as anything other than a side effect.

Use :bn and friends when you actually work with buffers and :n and friends when you work with the argument list.

like image 41
romainl Avatar answered Sep 30 '22 18:09

romainl