There are several options related to file backups in Vim/Nvim.
How are these options related to each other?
backup controls whether to make a physical backup when writing a
writebackup makes sure that a file backup be made before
overwriting a file. The backup will be removed when the file is successfully
backup option is also on. It reduces the risk that your file
is destroyed if something goes wrong when you are writing the file to disk. For
more info about using
writebackup together, see
backext controls the extension used for backup files.
will be appended to the backup file names (default is
~). For example, if
your file is
.bak, the backup file name will be
backupdir controls where the backup file will be placed. It is a
list of directories (comma separated) to put the backup files. The first item
in this option will be used. You must make sure that the backup directory
exists, or you will see the following error when writing a file:
E510: Can’t make backup file (add ! to override)
Use the following setting to deal with this error:
let g:backupdir=expand(stdpath('data') . '/backup') if !isdirectory(g:backupdir) mkdir(g:backupdir, "p") endif let &backupdir=g:backupdir
There is also an option
backupcopy, which needs special attention. The value
backupcopy can be
yes: it will copy the original file to the backup location and overwrite the original file.
no: it will rename the original file (i.e., move the original file to the backup directory) and write a new file with the same name.
auto: Nvim will choose what works best for you.
Note that for some applications, you may want to set
License CC BY-NC-ND 4.0