Backups with Vim

Vim helps protect you from unintentionally losing data by letting you make a backup of the files you edit. For an edit session that has gone terribly wrong, this can be useful because you can recover your previous file.

Backups are controlled by the settings of two options: backup and writebackup. Where and how backups are created are controlled by four other options: backupskip, backupcopy, backupdir, and backupext.

If both the backup and writebackup options are off (i.e., nobackup and nowritebackup), Vim makes no backup files for your edit sessions. If backup is on, Vim deletes any old backups and creates a backup for the current file. If backup is off and writebackup is on, Vim creates a backup file for the duration of the edit session and deletes the backup afterward.

The backupdir is a comma-separated list of directories in which Vim creates backup files. For example, if you want backups to always be created in your system’s temporary directory, set backupdir to "C:\TEMP" for Windows or "/tmp" for Unix and Linux.


If you’d like to always create a backup of your file in the current directory, you can specify “.” (a dot) as your backup directory. Or you could try to create a backup in a hidden subdirectory first if it exists, and then in the current directory if the hidden subdirectory doesn’t exist. Do this by defining backupdir’s value to be something such as "./.mybackups,." (the single dot at the end denotes the file’s current directory). This is a flexible ...

Get Learning the vi and Vim Editors, 7th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.