How do I make Vim automatically reload a file when it changes on disk?
Answer
:set autoread
Explanation
The autoread option tells Vim to automatically re-read a file when it detects the file has been changed outside of Vim. This is especially useful when working with build tools, git operations, or other processes that modify files you have open.
How it works
:set autoreadenables automatic file reloading- When Vim gains focus or you run an external command, it checks if open files have changed
- If a file changed and has no unsaved modifications in Vim, it is silently reloaded
- If the buffer has unsaved changes, Vim warns you instead of overwriting
Example
Add to your vimrc for permanent use:
set autoread
" Trigger autoread when changing buffers or coming back to Vim
au FocusGained,BufEnter * checktime
Now when a teammate pushes changes and you git pull in another terminal, your open files update automatically.
Tips
:checktimemanually triggers the external change check- The
FocusGainedautocommand makes this work better in terminal Vim - Use
:set autoread?to check the current setting - For the opposite behavior,
:set noautoreaddisables it