Vim backupcopy choice for retry-safe writes on network filesystems
0 reputation · 25 Nov 2020, 04:46 UTC
0 reputation · 25 Nov 2020, 04:46 UTC
I need to configure Vim so that an interrupted write to a shared network filesystem leaves the original file intact, while also ensuring that hard links to that file remain valid after a successful save. The documented behavior indicates that writebackup creates a temporary backup before overwriting, and backupcopy determines whether Vim copies the original or renames it to a backup name. When backupcopy is set to auto, Vim may choose rename-based writes on some systems and copy-based writes on others, which introduces uncertainty about whether inode numbers and hard links survive.
The core tension is that rename-based writes avoid momentarily duplicating the full file contents, yet they replace the original inode and break existing hard links. Copy-based writes preserve links but temporarily duplicate data, and some network filesystems may not support the rename semantics atomically. Because behavior can differ across Vim versions and operating systems, I am unsure which documented setting gives a predictable, retry-safe result without silently changing link relationships.
Can Vim be configured to use a single backupcopy strategy that both survives interrupted writes and preserves hard links on filesystems where rename is not atomic? Which option value should be preferred when the priority is avoiding duplicate data rather than preserving inode identity? Does the automatic choice differ between regular files and symlinked targets in a way that can be relied upon across versions?
35775 reputation · 25 Nov 2020, 09:25 UTC
To ensure that interrupted writes leave the original file intact while preserving hard links on network filesystems, you must use the following settings in your .vimrc:
set writebackup
set backupcopy=yes
When writebackup is enabled, Vim creates a backup of the file before overwriting it. The backupcopy option determines how that backup is created. By setting backupcopy=yes, you force Vim to copy the original file to a backup location rather than renaming it.
If your priority is avoiding the temporary duplication of data rather than preserving inode identity, use set backupcopy=no. This uses a rename-based strategy: Vim renames the original file to a backup name and creates a new file for the save. This is more efficient for large files but breaks all hard links and relies on the filesystem's rename() atomicity, which is often unreliable on older network shares (like some NFS versions).
backupcopy=autoThe auto setting allows Vim to choose the strategy based on the filesystem. This introduces unpredictability across different operating systems or Vim versions, making it unsuitable for environments where hard-link stability is a requirement.
Vim treats symlink targets as the underlying regular file. There is no version-stable difference in how backupcopy handles a symlink target versus a direct file path; the same inode rules apply to the target file.
You can verify that your configuration is preserving inodes using the following method:
touch test.txt && ln test.txt link.txt.ls -i test.txt link.txt (they should be identical).test.txt in Vim with backupcopy=yes, modify it, and save.ls -i test.txt link.txt again. The inode numbers should remain unchanged.Diagnostic Note: Please specify the network filesystem protocol (e.g., NFSv3, NFSv4, SMB) if you continue to see file corruption, as some protocols handle file locking and write-atomicity differently regardless of Vim's settings.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.