Too many inotify events when editing in vim

I am trying to use inotifywait to control specific folders and, if necessary, recompile. The problem is that I actively use vim, and when I edit vim, any modified file actually causes some "redundant" events, for example:

 :w sass/somefolder/ CREATE 4913 sass/somefolder/ CREATE some sass/somefolder/ MODIFY some 

It took me a while to realize that everything was really fine with inotifywait - I tried to use nano and everything worked as expected, only "MODIFY" starts and only once.

I tried to edit (for test purposes only, do not judge me) Emacs, and there are problems with Emacs - every time I press the triggers Ctrl-X + Ctrl + S MODIFY 3 times.

The question is, how can I solve problems with extra events in vim?

By the way, the directory and backupdir in my .vimrc not in the controlled folder.

UPD: This link explains why everything actually happens the way they happen, but I still don't know how to fix it. Well, of course, I can ignore the 4913 containing the string, but this is too stupid even for those trying to use inotify to compile SASS)))

UPD: VIM version - 7.3.429

+8
vim inotify
source share
3 answers

If you want to activate an action (for example, recompile the code) after editing the file, you usually want to watch the IN_CLOSE_WRITE event and ignore everything else.

You absolutely do not want to track IN_MODIFY events, because, as you have discovered, they can be triggered many times when editing a file.

So:

 inotifywait -e close_write ... 
+7
source share

The best editors do this in such a way as to ensure atomicity. In other words, if you do not die at the right time, you will not receive a half-recorded file.

One option that may help is to simply use autocmd BufWritePost to do its recompilation.

However, if you make other changes outside of vim, you probably want to wait for a few notifications and compile after some time has not happened, say, half a second. This will cover other unforeseen situations, such as, for example, performing source control.

+2
source share

Most editors will use a temporary file to record information about deleting or editing files before committing and saving. In addition, most editors need to write a temporary file to talk to sub-shells and scripts. I suspect that file 4913 may be a factor in your vim installation or a function of your numeric user ID to identify files.

You can bind vim to see when files are updated, and what happens on each side, for example. fork + exec is used, or another file that can tell which macro or object calls this.

+1
source share

All Articles