8

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?

6
  • What do you want to happen if you've both changed the same line? Mar 24, 2015 at 11:47
  • @Carpetsmoker Hmmm... Hard one. Maybe it could just pop up a message or something. I'm open to suggestions on that. Mar 24, 2015 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? Mar 25, 2015 at 14:50
  • @JavierScappini Either. I'm not that picky. I guess it would be cool to have it automatically load the changes... Mar 25, 2015 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? Mar 25, 2015 at 21:05

3 Answers 3

6

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.

1
4

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
2

There are some modes (eg in ex mode, entered by a command starting with :) in which the check shouldn't fire.

Here's what I came up with to avoid checks in unwanted modes:

" Trigger `checktime` when files changes on disk
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI *
        \ if mode() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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