edi9999
edi9999

Reputation: 20574

Vim is triggering multiple saves

I have been encountering a problem using nodemon together with vim

nodemon is a deamon that watches files and starts a script every time the file changes.

Strangely, when I run nodemon and save a file with vim, nodemon detects two filechanges of the file.

You can replicate this issue with the following snippet:

npm install nodemon -g
echo "console.log('hello world');" > server.js
nodemon server.js -V
vim server.js

Than try to save the file (with :w)

It seems that everytime I save a file from vim, the watch gets triggered twice. However, if you open server.js with pico, the file changes only once.

I don't think the problem lies with nodemon, so I'm asking here what could create this particular behaviour ?

I have also tried to disable all vim plubin vim -u NONE server.js but this didn't help.

They also is a corresponding issue on github: https://github.com/remy/nodemon/issues/349, however, it doesn't seem easy to know what is happening.

Upvotes: 6

Views: 620

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172748

This is due to the file write handling of Vim. See :help 'backupcopy' for an explanation. Editors like Vim replace the original file with a temporary backup to avoid losing the file contents completely. This is also an issue when watching the file for changes via inotifywait (see here). A workaround for that is to

:set backupcopy=yes

You'll still see events for the backup file, but at least it's for another file. To completely forego the safety of a backup, you can addditionally

:set nobackup

Upvotes: 3

Related Questions