Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim is triggering multiple saves

Tags:

vim

save

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.

like image 236
edi9999 Avatar asked Jan 05 '15 15:01

edi9999


1 Answers

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
like image 183
Ingo Karkat Avatar answered Oct 03 '22 18:10

Ingo Karkat