Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make vim check automatically if the file has changed externally?

Tags:

vim

I usually open many files in tabs with vim -p. Is it possible to check if any of the files was changed outside of Vim since editing started?

like image 713
Eugene Yarmash Avatar asked Feb 20 '10 19:02

Eugene Yarmash


2 Answers

Add these lines to your .vimrc:

au FocusGained,BufEnter * :silent! checktime
au FocusLost,WinLeave * :silent! w

Basically, check for and reload (or discard) external changes when Vim or the current buffer gains focus, and optionally, auto-save when leaving focus.

Source: Vim Wiki.

like image 69
Eugene Yarmash Avatar answered Nov 15 '22 19:11

Eugene Yarmash


I came across an interesting find related to this question today...

Hidden in /usr/share/vim/vim71/vimrc_example.vim there is this command:

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
        \ | wincmd p | diffthis

It will open a vimdiff-like window with the current buffer and the underlying file highlighting all of the changes between the two.

like image 28
ezpz Avatar answered Nov 15 '22 18:11

ezpz