sponsor Vim development Vim logo Vim Book Ad

Atomic Save : Save files atomically with ordinary "w" command

 script karma  Rating 14/7, Downloaded by 1297  Comments, bugs, improvements  Vim wiki

created by
Andrew Cady
 
script type
utility
 
description
This is an atomic save function for vim.  IOW, this is an atomic write ('w') function for vim.  It makes sure that there is always a fully-written copy of the file in place to be read by other programs.  It uses the system 'mv' since vim's rename() function removes the destination file rather than atomically replacing it.

To use it, you enable it as described below, and then 'w' your file like normal.

NOTE: if using windows, see the note below.
 
install details
Put the function in your .vimrc, also after that, add this line to save all files atomically:

autocmd BufWriteCmd * call AtomicSave()

Or add a line like this, which will only save files named ".procmailrc" atomically:

autocmd BufWriteCmd .procmailrc call AtomicSave()

Type :help autocmd for more info on how you can choose when to use this function

NOTE: If you are using windows, you probably need to change "mv" to "rename" -- unless you have installed GNU coreutils deep in the bowels of your microsoft OS.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
AtomicSave.vim 0.9.3 2017-07-03 7.0 Andrew Cady Preserve owner and group information on save
AtomicSave.vim 0.9.2 2014-01-24 5.7 Andrew Cady Fixed bug where symlinks were not preserved.  Writing to a symlink would replace the link with a regular file.  Now it replaces the symlink's target.
AtomicSave.vim 0.9.1 2012-12-20 5.7 Andrew Cady This version will preserve file permissions on the saved file using GNU chmod.  If your system does not have GNU chmod, it will fail.

Another change: it will now produce a status message indicating success, after writing the file.
AtomicSave.vim 0.9 2011-06-24 5.7 Andrew Cady Initial upload
ip used for rating: 44.197.251.102

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github