When I'm in a modified buffer and try to open a file using :e otherFile I get

E37: No write since last change (add ! to override)

Since this happens fairly often, I'd like to automatically save the current file before opening the new one.

That's why I added this to my vimrc:

autocmd BufLeave * silent! wall

But this autocommand doesn't help when using :e otherFile.

NVIM v0.4.3

  • 1
    Why not use :h 'hidden' ? – dedowsdi Nov 13 '19 at 8:28
  • Thanks for your comment @dedowsdi! Can you describe the difference between :set hidden and :set autowriteall in an answer? – Matthias Braun Nov 13 '19 at 9:37
  • Sorry, don't have time for that. You can read :h 'hidden' and :h 'autowriteall', then try both (make sure only one of them is set), you should see the difference immediately, won't cost more 15 mins. – dedowsdi Nov 13 '19 at 10:40
  • I read the docs and tried both hidden and autowriteall alternatingly but couldn't tell the difference in behavior. In both cases the buffer that I was leaving was saved. – Matthias Braun Nov 13 '19 at 14:37
  • You must make sure only one of them is on. 'hidden' won't work if 'autowrite' or 'autowriteall' is on. – dedowsdi Nov 13 '19 at 22:41

The usual solution is

set hidden

This allows a buffer to be made « hidden », i.e., not physically shown in a window, while still unsaved.

Without 'hidden' on, vim’s default behavior is to not allow closing a modified buffer without being forced (via !s on commands). 'hidden' allows a third state between open and closed.

Do note that 'confirm' may be useful here; I believe it instructs vim to confirm, when you quit, the save/don’t save state of any modified buffers.

| improve this answer | |

One way to achieve this is to

:set autwriteall

This will save the file before switching to another and also save it before quitting Vim.

Gleaned from :help autowrite and :help autowriteall:

Write the contents of the file, if it has been modified, on each next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!, make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I command takes you to a new file. Also write when using :edit, :enew, :quit, :qall, :exit, :xit, :recover and closing the Vim window.

| improve this answer | |

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.