How do I make Vim automatically reload files changed outside the editor?
Answer
:set autoread | autocmd FocusGained * checktime
Explanation
When working with Git, build tools, or collaborators, files may change outside Vim. The autoread option combined with FocusGained autocmd ensures Vim detects and reloads these changes automatically when you return to the editor.
How it works
autoread— tells Vim to re-read files that changed on disk (when Vim notices)checktime— explicitly tells Vim to check all buffers for external changesFocusGained— fires when the terminal/GUI regains focusCursorHold— fires when the cursor is idle, another good trigger
Example
set autoread
autocmd FocusGained,BufEnter * checktime
autocmd CursorHold,CursorHoldI * checktime
1. You're editing file.py in Vim
2. A git pull modifies file.py externally
3. You switch back to Vim (FocusGained fires)
4. checktime detects the change
5. File is silently reloaded with the new content
Tips
autoreadalone isn't enough — Vim only checks on specific triggers, hence the autocmds- If the file changed AND you have unsaved edits, Vim will prompt you
- Set
updatetime=250for fasterCursorHolddetection (default is 4000ms) - Use
:checktimemanually to force an immediate check of all buffers