Vim 'autowriteall' option only for specific buffers

Tag: vim Author: ckgirl9999 Date: 2009-07-14

The 'autowriteall' option makes Vim save all buffers when quitting. I want to be able to use this option only for one specific buffer (a temp file which will be discarded soon) but not for the other buffers.

How do I get Vim to automatically save changes only for one specific buffer?

Other Answer1

It's not quite perfect, but one option would be to use the VimLeavePre autocmd:

:autocmd VimLeavePre <buffer> w

However, you'll have to quit with :q! or :qa! for this to work, otherwise it'll never get as far as initiating the autocmd.

:help autocmd
:help VimLeavePre
:help autocmd-buffer-local

Other Answer2

You're going to have to use a combination of autocommands. The immediately obvious relevant ones are:

  • BufHidden
  • BufLeave
  • BufUnload
  • BufDelete

This will cover hiding buffers, leaving them for other buffers or windows, closing Vim, and deleting buffers. (I think BufDelete is redundant given BufUnload but I'm not totally sure I've considered all cases). Note that VimLeavePre will only work if the buffer you're trying to save is the active one, so it's not what you want.

The template autocommand is going to be

:autocommand {event} {filename} w

Or, if you don't have an easy filename pattern to match or it might not have one at all (in which case the w command will need a filename argument) you can use buffer-local autocommands. These would probably have to be set somehow when creating the buffer, like if it's one spawned by some script to show some certain information. For information on this, see:

:help autocmd-buffer-local

You can get information about the multitude of autocommand events from

:help autocommand-events

Other Answer3

maybe what you want is:

setlocal autowriteall

setlocal only enables a function for the specified buffer. autowriteall is autowrite + save on quit, enew, e and others (h autowriteall)


autowriteall is a global option.
No, autowriteall can only be set globally, even if you use 'setlocal' it is set globally.