There are several options related to back up in Vim/Nvim.

  • backup
  • writebackup
  • backupdir
  • backupcopy
  • backupext

The option backup controls whether to make a physical backup when writing files.

The option writebackup make a backup before overwriting a file. If the backup will be removed when the file is successfully written, unless backup option is also on. For more info about using backup and writebackup together, see :h backup-table. It reduces the risk that you file is destroyed if something goes wrong when you are writing the file.

The option backext control the extension used for backup files. backupext will be appened to the file names and the default is ~. For example, if your file is test.txt and backupext is .bak, the backup file name will be test.txt.bak.

The option backupdir controls where the backup file will be put. It is a list of directories to put the backup file separated by comma. The first item in this option will be used. You must make sure that the backup directory exist, or you will meet the following error when you want to write a file:

E510: Can’t make backup file (add ! to override)

let g:backupdir=expand(stdpath('data') . '/backup')
if !isdirectory(g:backupdir)
   mkdir(g:backupdir, "p")
let &backupdir=g:backupdir

There is also an option backupcopy which need special attention. The value of backupcopy can be yes, no or auto:

  • yes: it will 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 backupcopy to yes to avoid issues.