I have a config-file that I keep open in vim, but that sometimes gets changed on disk, without these changes being reflected on the terminal. Can I refresh the content on the screen without closing and re-opening the file? If so, how?
You can use the
:edit command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use
:edit! to force the reload of the current file (you will lose your
:edit can be abbreviated by
:e. The force-edit can thus be done by
In addition to manually refreshing the file with
:edit, you can put into your
to make Vim automatically refresh any files that haven't been edited by Vim. Also see
Skip to the
Wrap-up heading for the
vimrc lines to add to do make your life better.
Check if any buffers were changed outside of Vim. This checks and warns you if you would end up with two versions of a file.
To do automatically load changes, add in your
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. When the file has been deleted this is not done.
This answer adds a caveat:
Autoread does not reload file unless you do something like run external command (like
Read on for solutions.
Trigger when cursor stops moving
Add to your
au CursorHold,CursorHoldI * checktime
Trigger on buffer change or terminal focus
Add the following to your
vimrc to trigger
autoread when changing buffers while inside vim:
au FocusGained,BufEnter * :checktime
Catching terminal window focus inside plain vim
FocusGained (see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin:
On tmux versions > 1.9, you'll need to add in
set -g focus-events on
Here are my
vimrc lines to implement all the above:
" Triger `autoread` when files changes on disk " https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044 " https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif " Notification after file change " https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread autocmd FileChangedShellPost * \ echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None