7

Before I heard about vim, I used to use gedit. I still try to make vim behave as same as gedit, this is because I have asked many questions related to vim on StackOverflow.

One feature I am missing is when any file was modified while I was working on any file on gedit by another application, a popup use to come which says The file <file_location> changed on disk. Do you want to reload the file? And there were two buttons named Reload and Cancel respectively.


What I want:

(Please note that I am using vim, not gvim) I want similar feature in vim. I want if any file get changed on disk, a warning message come at status bar:

File changed, press F9 to reload.

I will map my F9 to do :e.

Community
  • 1
  • 1
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
  • 1
    Right now it will warn you in the statusline, but only if you attempt to save new changes. And I suppose you already found out that gVim _does_ send a popup when a file changes externally – Michael Berkowski Aug 23 '12 at 12:24

2 Answers2

6

If autoread is set, vim checks whether the file has been modified after each shell command (:!), on writing the file, and when you issue :checktime. gvim in addition checks when you switch window focus to the application.

You can execute :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 just print a warning, set the autocmd FileChangedShell (Detect file change, offer to reload file):

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

For Insert mode, use CursorHoldI (not sure about this feedkeys sequence, but it seems to work):

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

You might have to change the FileChangedShell autocmd from echo to echoe, as I don't think echo gets printed in Insert mode.

Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • So, how do I make vim check every 5 seconds? – Santosh Kumar Aug 23 '12 at 13:39
  • Can you give full configuration? – Santosh Kumar Aug 23 '12 at 13:41
  • Heyy.. I did not wanted to update automatically. I want to show warning at status bar only. – Santosh Kumar Aug 23 '12 at 14:24
  • A silly question. Will doing all these give my RAM or CPU more pressure than before? If yes, how much? – Santosh Kumar Aug 23 '12 at 14:43
  • You'll be waking up the CPU every 5 seconds, so that'll have a power draw. Should be pretty insignificant, though; it doesn't need to go to disk as the OS will have the information cached, and your computer has more frequent wakeups anyway. – ecatmur Aug 23 '12 at 14:45
  • Some weird things are happening. File is updating automatically in normal mode *(I expected to just show warning, by the way its ok)*. The warning is not shown in insert mode, even nothing is happening when in insert mode *(I expected atleast to get notified when there is a file change)*. What do I do? – Santosh Kumar Aug 24 '12 at 16:49
  • You can use `CursorHoldI` to run an autocmd in Insert mode, but it won't be safe to use the same `feedkeys` sequence. – ecatmur Aug 24 '12 at 16:58
  • @Santosh also if you don't want the file to update automatically, turn off `autoread`. – ecatmur Aug 24 '12 at 17:06
  • Can you tell me what should be feedkeys for insert mode? – Santosh Kumar Aug 24 '12 at 17:12
  • What is difference between `function` and `function!`? – Santosh Kumar Aug 24 '12 at 19:49
  • @Santosh `function!` overwrites an existing function. See http://vimdoc.sourceforge.net/htmldoc/eval.html#user-functions – ecatmur Aug 24 '12 at 19:57
0

Trying to force Vim into working like another editor is a pointless exercise. I've been guilty of that (with TextMate as a model) and it has been both a complete failure and an obstacle on the learning curve.

However, what you are asking — notification on external change — is the default behavior which can be reversed with :set autoread.

romainl
  • 186,200
  • 21
  • 280
  • 313