7

I've read that most editors will replace the file when you actually want to save changes to that file: How to execute a command whenever a file changes?

How does Vim behave?

math
  • 2,693

2 Answers2

6

it might depend on various settings related to 'backup', everything that has to keep a copy of the file as it was before the write happened. one of settings that controls that is: 'backupcopy':

When writing a file and a backup is made, this option tells how it's done. This is a comma separated list of words.

The main values are:

"yes"    make a copy of the file and overwrite the original one
"no"     rename the file and write a new one
"auto"   one of the previous, what works best

so, depending on your vimrc (backup set and backupcopy to no), vim might rename a file. when

akira
  • 63,447
1

I've observed that the option writebackup and whether the edited file has more than one hard link changes the behavior of Vim in that regard.

The following results pertain to Vim 9.0.2167 and Neovim 0.9.4 on Arch Linux using ext4 when saving a file with :w.

In case writebackup is on (which is the default)

  • Editing a file with only one hard link to it:

    • inode changes (file is recreated)
    • changes timestamps Access, Modify, Change, and Birth
  • Editing file with multiple hard links:

    • inode stays the same
    • hard links stay intact
    • Birth timestamp remains the same. Access, Modify, and Change timestamps do change

In case writebackup is off (:set nowritebackup)

  • inode stays the same
  • only Modify and Change timestamps change. Access and Birth remain the same

See also the backup option:

If you write to an existing file (but do not append) while the 'backup', 'writebackup' or 'patchmode' option is on, a backup of the original file is made. The file is either copied or renamed (see 'backupcopy'). After the file has been successfully written and when the 'writebackup' option is on and the 'backup' option is off, the backup file is deleted. When the 'patchmode' option is on the backup file may be renamed.

'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