Vim: difference between: n and: bn

If I started vim with several files, such as vim *.java , I can switch through open files with :n or :bn (and other related commands).

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

What is the difference between these two situations? If I do: buffers in both cases, then there is no difference in the list of buffers. It may seem like I'm asking an unnecessary question, but I would like to understand if there are any distortions lurking under the hood. Thank you

+7
vim
source share
2 answers

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

Explanation

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

:bn abbreviated for :bnext , which moves in the buffer list

Opening a file with :sp foo does not change the argument list, but adds a buffer and therefore does not change the behavior of :n , but affects :bn .

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

Session Example:

 $ vim /tmp/foo /tmp/bar :args [foo] bar :buffers 1 %a "foo" line 1 2 "bar" line 0 

Here the list of buffer and arguments matches

 :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 list of arguments 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 has been replaced, and the buffer list has been expanded.

+7
source share

: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, each file in the argument list is loaded into the buffer and thus added to the buffer list, but although changes in the argument list may have some effect on the buffer list, changes in the buffer list will never have any effect in the argument list. This means that the two lists may be equivalent, but there is no guarantee.

:n can load the next buffer from the list of buffers, and :bn can load the next file in the argument list, but this should not be considered as anything other than a side effect.

Use :bn and friends when you really work with buffers and :n and friends when you work with a list of arguments.

+3
source share

All Articles