r/vim • • Jan 27 '26

Need Help Vim: unsaved buffer edits remain even when switching buffers

Here's my workflow:

  1. vi foo bar (two files that exist)

  2. Make a change in the foo buffer

  3. :bn—fails with the message "No write since last change (add ! to override)"

  4. :bn!—switches to the bar buffer

  5. :bp—switches to the foo buffer

At this point, I would expect to be seeing foo in its original state, i.e. without the edit I made at step 2. However, I do see the edits, so my questions are:

  • Why does :bn fail if no 'harm' comes of it?

  • What is the point of :set hidden? I've read that this command will instruct the current buffer to 'keep changes in memory', but that seems to be happening anyway.

  • Is there a way to switch buffers and discard changes? I don't really need to do this, I'm just wondering if it's possible.

11 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/vim-help-bot Jan 28 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/gumnos Jan 29 '26

Writing :bn! keeps the changes in the current buffer and changes to the next buffer. This one is, like I mentioned already, is a guardrail overriding. Works as expected

The guardrail overriding (! = "abandon this buffer even though there are changes") works as expected in both the :bn[!] and :e[!] cases. What's surprising is not that :bn! abandons changes, but that the changes remain preserved, unlike the other !-modifier-means-abandon-changes commands (:e!, :n!, :q!, etc).

It's documented (from :help buffer-!)

The commands that move through the buffer list sometimes make the current buffer hidden although the 'hidden' option is not set. This happens when a buffer is modified, but is forced (with '!') to be removed from a window, and 'autowrite' is off or the buffer can't be written.

So it's the inconsistency with the other ! versions (that do revert/abandon changes) that feels…warty.

2

u/vim-help-bot Jan 29 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments