12

I hear somewhere that if backup is disabled and writebackup is enabled, Vim will create backup files for the duration of the edit session and will delete these backup files afterward. However, even with these settings, i.e.

set nobackup
set writebackup " Enabled!
set noswapfile
set noundofile  

I don't see any additional files created.

3
  • 1
    What behavior are you looking for exactly? nobackup with writebackup means that the backup file is immediately deleted after a successful write.
    – ZeroKnight
    Jul 22, 2018 at 11:39
  • @ZeroKnight So, as I understand the information here about nowritebackup is not correct?
    – john c. j.
    Jul 22, 2018 at 11:49
  • 1
    The line about backup files persisting until the session ends is incorrect. They're either transient or kept until you delete them, depending on the backup setting. Take a look at :h backup-table.
    – ZeroKnight
    Jul 22, 2018 at 12:01

2 Answers 2

21

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.

3
  • 2
    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.
    – KRyan
    Jul 22, 2018 at 22:35
  • "write is not used as a verb, but as an adjective". That's brilliant.
    – ZeroKnight
    Jul 22, 2018 at 22:39
  • Glad you like it! Feel free to incorporate it into your answer if you wish.
    – KRyan
    Jul 22, 2018 at 22:40
4

As @ZeroKnight said with :set nobackup writebackup the backup file will be used during :write but will be deleted afterwards. The official documentation is here: backup and writebackup.

For the question (my emphasis)

What does nowritebackup actually do?

You can use inotify to find out. Make a test dir and save the settings you specified above as a test vimrc file, then write a file to that test directory and watch the output of inotify. Like this:

first terminal window

mkdir test-dir
cat > test-dir/vimrc <<EOF
set nobackup
set writebackup " Enabled!
set noswapfile
set noundofile
EOF
inotifywait -m -r test-dir

second terminal window

vim test-dir/foo
:w

For me the output looks like this:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
test-dir/ OPEN foo
test-dir/ CREATE foo~
test-dir/ OPEN foo~
test-dir/ ATTRIB foo~
test-dir/ CLOSE_WRITE,CLOSE foo~
test-dir/ ATTRIB foo~
test-dir/ CLOSE_NOWRITE,CLOSE foo
test-dir/ OPEN foo
test-dir/ MODIFY foo
test-dir/ ATTRIB foo
test-dir/ CLOSE_WRITE,CLOSE foo
test-dir/ DELETE foo~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.