2

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.


Using Neovim v0.4.3

5
  • 1
    Why not use :h 'hidden' ?
    – dedowsdi
    Nov 13, 2019 at 8:28
  • Thanks for your comment @dedowsdi! Can you describe the difference between :set hidden and :set autowriteall in an answer? Nov 13, 2019 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, 2019 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. Nov 13, 2019 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, 2019 at 22:41

2 Answers 2

2

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.

2

One way to achieve this is to

:set autowriteall

This will save the file before switching to another and also save it before quitting Vim. So every time you type commands like :bn, short form for :bnext, the buffer will be saved automatically and the buffer changed, in this case for the next one in a circular manner.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.