2

I'm trying to understand and eventually fix the following behavior: when I save a file using VIM, it triggers the change/write event twice.

I'm using Ubuntu 16.10, and I created an empty .vimrc file to make sure that no plugins create this behavior.

So, I open the foo.txt file:

vim -u .vimrc foo.txt

And then using inotify-tools (installed using sudo apt install inotify-tools) I run the following command:

inotifywait -m foo.txt

Then in VIM I do :w, triggering the save. I can see the following output:

$ inotifywait -m foo.txt 
Setting up watches.
Watches established.
foo.txt OPEN 
foo.txt ACCESS 
foo.txt CLOSE_NOWRITE,CLOSE 
foo.txt MODIFY 
foo.txt OPEN 
foo.txt MODIFY 
foo.txt CLOSE_WRITE,CLOSE 
foo.txt ATTRIB 

After setting backupcopy=yes I can see EXACTLY the same behavior. I tried to add it in the vimrc and restart VIM, but there was no change at all.

How can I fix this?

Everyone on the internet recommends using backupcopy=yes which for some reason doesn't fix the issue on my machine. Why?

Is there any other workaround?


In my original ~/.vimrc I do have this at the end of the file:

" Node.js fs.watch
set backupdir=~/.vim/tmp,.
set directory=~/.vim/tmp,.
set backupcopy=yes

Doesn't seem to make any change (I do have the ~/.vim/tmp dir).

  • Did you set backup as well? – laktak Mar 3 '17 at 20:02
  • @laktak Not sure about the backup you're mentioning, but I do have backupdir and directory, see the edit. – Ionică Bizău Mar 4 '17 at 19:29
1

If you have backupcopy=no Vim will try to rename the file. If this fails it will work the same way as backupcopy=yes.

For example (with strace):

rename("test.txt", "/tmp/test.txt~") = -1 EXDEV (Invalid cross-device link)

But in any case, Vim will only write to the file once:

set backupcopy=no

rename("test.txt", "test.txt~")   = 0
open("test.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 3
write(3, "a\nbb\n\nccc\ndddd\n\neeeee\n", 22) = 22
close(3)                                = 0


set backupcopy=yes

open("test.txt", O_RDONLY)           = 3
open("test.txt~", O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0644) = 5
read(3, "a\nbb\n\nccc\ndddd\n\neeeee\n", 8192) = 22
write(5, "a\nbb\n\nccc\ndddd\n\neeeee\n", 22) = 22
read(3, "", 8192)                       = 0
close(5)                                = 0
close(3)                                = 0
open("test.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 3
write(3, "a\nbb\n\nccc\ndddd\n\neeeee\n", 22) = 22
close(3)                                = 0

Maybe you have a plugin/autocmd that is responsible for the second write. Test with vim -u NONE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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