Vim Backup Files vs Swap Files: Should Vim Auto‑Delete Backups After Recovery?
0 reputation · 06 Jun 2024, 05:58 UTC
0 reputation · 06 Jun 2024, 05:58 UTC
The goal is to confirm that the data recovered by Vim after a crash matches the edits intended by the user, while ensuring that the backup file created by the 'backup' option no longer contains the pre‑edit state.
When 'backup' is enabled, Vim creates <file>~ before writing the new content, and a swap file (<file>.swp) holds the in‑memory changes. After a crash, Vim can recover the session from the swap file, but the backup file remains untouched unless the user explicitly removes it. This persistence raises the question of whether Vim should automatically delete the backup file once the recovery is verified.
1. Should Vim automatically delete <file>~ after a successful recovery?
2. If not, what conditions or command flags would trigger such deletion?
3. How can a user programmatically verify that the backup file no longer reflects the original content after recovery?
31775 reputation · 06 Jun 2024, 17:43 UTC
Should Vim automatically delete <file>~ after a successful recovery?
No. Vim keeps the backup file untouched after a crash recovery. The backup is only removed when a subsequent write operation overwrites it or when you explicitly delete it.
.swp) is the only file Vim removes automatically on a normal exit; it is left after a crash.help backup
help backupcopy
help backupdir
set backup, Vim writes <file>~ before saving the new content.set backupcopy=yes, the backup file is overwritten by the new content, effectively deleting the old backup.set backupcopy=no (default), Vim renames the old file to <file>~ and creates a new backup on the next write.set backup enabled.kill -9 $$ from another terminal) while editing.diff <file> <file>~
# If diff shows differences, the backup still contains the pre‑edit state.
If you prefer Vim to keep only the most recent backup, add this to your vimrc:
set backup
set backupcopy=yes
set backupdir=$HOME/.vim/backup
backupdir can point to a temporary or low‑space directory.backupcopy=yes, the old backup is overwritten, so only one backup file exists per original file.find $HOME/.vim/backup -type f -mtime +30 -delete
Do you want Vim to keep a history of backups (e.g., for audit or rollback purposes) or would you prefer it to overwrite them automatically? This choice determines whether backupcopy=yes is suitable for your workflow.
Use comments to ask for clarification. Post a solution as an answer.
31,775 reputation · 06 Jun 2024, 15:57 UTC
After Vim recovers from a crash using the swap file, the backup file (file~) remains unchanged, preserving the pre‑edit state even though the edited file now reflects the recovered changes. This means the backup and the recovered file can diverge: the backup still contains the original content, while the file contains the user's intended edits. To verify, run diff file file~; a non‑empty output confirms the backup still holds the pre‑edit version. Users who want the backup overwritten on each write can set backupcopy=yes, but the default retains both files indefinitely. In Neovim, the hiddenbackup option allows deferred writing without altering this recovery behavior.