Art
Art

Reputation: 24607

Current buffer in Vim get replaced when opening a file

When opening a new file via :E command (not with ctrl-P plugin, this works fine!), both Vim and MacVim (8.0.1098) discard contents of the current buffer, so it becomes impossible to switch to if by using :bn! and :bp! commands.

However, if I modify the contents of current buffer, then open another file and only then try to close MacVim, I get a warning message about unsaved file. So the buffer must be still hanging somewhere?

Also, if I split the window and open a file then, both buffers remain open.

I do have set hidden in my .vimrc and running :set hidden? returns hidden, meaning that scrolling through hidden buffers with :bn! and :bp! should work, but it doesn't.

Upvotes: 1

Views: 653

Answers (2)

Art
Art

Reputation: 24607

The fact that ctrlP had no issues opening & switching buffers suggested there was something wrong with netrw.

Upon closer inspection it turned out that my netrw plugin was out-of-date.

Upgrading to v162j (via Vundle) solved the problem.

Upvotes: 0

romainl
romainl

Reputation: 196876

When opening a new file via :E command, both Vim and MacVim (8.0.1098) discard contents of the current buffer, so it becomes impossible to switch to if by using :bn! and :bp! commands.

The behavior you describe is inconsistent with:

  • the expected behavior,
  • and the behavior I get with the exact same version.

To test it in a repeatable way:

$ vim -Nu NORC a.txt     starts Vim with a.txt
:Explore                 replaces the current buffer with a netrw buffer
(press <CR> on b.txt)    replaces the netrw buffer with b.txt
:bn (or :bp)             replaces the current buffer with a.txt

However, if I modify the contents of current buffer, then open another file and only then try to close MacVim, I get a warning message about unsaved file. So the buffer must be still hanging somewhere?

Yes, it's almost certainly in the buffer list. Try :ls for listed buffers and :ls! for all buffers. :ls should show you a.txt at #1 and b.txt at #3 whereas :ls! should show you the same, plus the netrw buffer at #2.

Also, see :help 'hidden'.

--- EDIT ---

For reference, the expected output of :ls! should be:

1      "a.txt"                        line 1
2u#h-  "~/"                           line 11
3 %a   "~/b.txt"                      line 1

Note that the only "unlisted" buffer should be the Netrw buffer: ~/ at #2.

Did you change anything to any Vim-related file or directory outside of $HOME?

Upvotes: 1

Related Questions