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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With