Vim 'autowriteall' option only for specific buffers

Vim 'autowriteall' option only for specific buffers

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?

Vim textwidth comma indent problem


vim and unrecognized characters
It's not quite perfect, but one option would be to use the VimLeavePre autocmd:.
emacs equivalent of vim's shift-h and shift-l
: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..
How can I execute a command when a buffer is closed in vim?
:help autocmd :help VimLeavePre :help autocmd-buffer-local 

The behavior of ; to repeat the last t command bothers me. Can you help me make it better?

How to fix Home and End in Vim?


vim left and right column highlight
You're going to have to use a combination of autocommands.

VIM: how to put result of a colon command into a variable?
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 


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).

85 out of 100 based on 65 user ratings 265 reviews