I have a situation where sometimes my friend and I are working in the same file at the same time.

Vim will tell me if the file has changed when I try to overwrite it. Can I have vim notify me as soon as the file changes, all by itself, before I save?

  • What do you want to happen if you've both changed the same line? – Martin Tournoij Mar 24 '15 at 11:47
  • @Carpetsmoker Hmmm... Hard one. Maybe it could just pop up a message or something. I'm open to suggestions on that. – Questionmark Mar 24 '15 at 18:20
  • But what would be the desired behavior you're expecting? That Vim not just reload the changes but to actually shows something like a desktop popup message or avoid Vim to automatically reloads the changes and shows a warning message instead letting you decide what to do? – Javier Scappini Mar 25 '15 at 14:50
  • @JavierScappini Either. I'm not that picky. I guess it would be cool to have it automatically load the changes... – Questionmark Mar 25 '15 at 15:22
  • My answer here has some info about the autoread setting & it's limitations ... possibly this is also an answer for this question? – Martin Tournoij Mar 25 '15 at 21:05
up vote 5 down vote accepted

Did you try the autoread option? From :help 'autoread':

When a file has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again.

This question has been answered in StackOverflow: How does Vim's autoread work?

Solution 1

Follow explanations in Vim Wikia page Have Vim check automatically if the file has changed externally. This solution requires the addition of a function to your vimrc file and that a new command is called on the buffers you want to watch.

Solution 2

This solution was proposed by @GregSexton in the question I linked above. The idea is to force the modification check whenever one of the following happens:

  • Entering the buffer.
  • No key was pressed for 'updatetime'
  • Cursor moved.

You can find the code that implements this below:

augroup checktime
    au!
    if !has("gui_running")
        "silent! necessary otherwise throws errors when using command
        "line window.
        autocmd BufEnter        * silent! checktime
        autocmd CursorHold      * silent! checktime
        autocmd CursorHoldI     * silent! checktime
        "these two _may_ slow things down. Remove if they do.
        autocmd CursorMoved     * silent! checktime
        autocmd CursorMovedI    * silent! checktime
    endif
augroup END

Add and/or remove autocmd as best suits your workflow. Check here for the list of events that can be used.

If autoread is also enabled, file will be automatically read if not modified:

set autoread

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.