How to receive notification when a file is modified by another application?

Before I found out about vim, I used gedit. I'm still trying to get vim to behave just like gedit, this is because I asked a lot of questions related to vim in StackOverflow.

One feature that I am missing is when a file was changed, when I worked on any file in gedit by another application, to use the pop-up that says The file <file_location> changed on disk. Do you want to reload the file? The file <file_location> changed on disk. Do you want to reload the file? . And there were two buttons with the name Reload and Cancel, respectively.


What I want:

(Note that I'm using vim, not gvim). I need a similar function in vim. I want if any file is modified on disk, a warning message will appear in the status bar:

 File changed, press F9 to reload. 

I will match my F9 :e .

+6
source share
2 answers

If autoread installed, vim checks to see if the file was modified after every shell command ( :! ), When writing the file and when issuing :checktime . gvim additionally checks when you switch the focus of the window to the application.

You can do :checktime periodically using the recipe at http://vim.wikia.com/wiki/Timer_to_execute_commands_periodically :

 autocmd CursorHold * call Timer() function! Timer() call feedkeys("f\e") checktime endfunction set updatetime=5000 " milliseconds 

To simply print a warning, set autocmd FileChangedShell ( Detect file change, FileChangedShell to reload file ):

 autocmd FileChangedShell * echo "File changed, press F9 to reload." 

In CursorHoldI mode, use CursorHoldI (not sure about this file sequence, but seems to work):

 autocmd CursorHoldI * call TimerI() function! TimerI() call feedkeys("\<CR>\e") checktime endfunction 

You may need to change FileChangedShell from echo to echoe since I don't think echo is printed in insert mode.

+6
source

Trying to get Vim to work like another editor is a meaningless exercise. I was to blame for this (with TextMate as a model), and this was both a complete failure and an obstacle to the learning curve.

However, what you are requesting - an external change notification - is the default behavior that can be changed to :set autoread .

0
source

Source: https://habr.com/ru/post/923554/


All Articles