backup
and writebackup
are two separate yet intertwined options; they are essentially two halves of the same whole. I think the easiest way to explain these two options is by detailing what happens when you attempt to overwrite a file with some combination of these options.
Scenario Breakdown
Assume you have an existing file foo.txt
. You make some changes to it (beyond just appending to it) and issue a :write
command...
If either backup
or writebackup
(or both) are enabled, then before writing the file, Vim will create a backup (location depends on the backupdir
setting). Afterwards, Vim will attempt to write the original file. At this point, behavior depends on the backup
setting:
- If
nobackup
, then the backup file is immediately deleted upon successfully writing the original file.
- If
backup
, then the backup file is kept afterwards and will be overwritten on future backups.
And that's it; behavior really depends on the backup
setting. It should be noted that backup
will override writebackup
in the case that the former is enabled and the latter is disabled, e.g. :set backup nowritebackup
. In other words, it is identical to :set backup writebackup
.
This table from :h backup-table
outlines the same thing:
*backup-table*
'backup' 'writebackup' action
off off no backup made
off on backup current file, deleted afterwards (default)
on off delete old backup, backup current file
on on delete old backup, backup current file
Regarding the option names
You may be thinking that the names of these options should be swapped, as each option's name seems to describe the other's more accurately. I originally held this same opinion, but I'm going to mirror KRyan's insightful comment here:
To help understand the naming convention, consider this: backup
always produces a backup, while writebackup
produces a backup during a write procedure. In other words, write is not used as a verb, but as an adjective describing the type of backup you’re enabling.
nobackup
withwritebackup
means that the backup file is immediately deleted after a successful write.nowritebackup
is not correct?backup
setting. Take a look at:h backup-table
.