430

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?

twan163
  • 5,590
  • 4
  • 14
  • 17
  • 3
    Related: [How can I reload all buffers at once?](http://vi.stackexchange.com/q/458/467) at Vim SE – kenorb Apr 18 '15 at 21:06
  • Wrap up: all the answers so far are of "Poll" instead of "Push" style. That is, instead of receiving an external file change event like it was done by other software's similar features, these answers rely on vim actively polling the file change, either manually or triggered by a user action or a timer. The end result is you won't see change refreshed on screen instantaneously. – Penghe Geng Jul 01 '19 at 18:06
  • Working solution at https://stackoverflow.com/a/53860166/4814774 – daGo Mar 31 '20 at 06:29
  • See also: [How does Vim's autoread work? - Stack Overflow](https://stackoverflow.com/questions/2490227/how-does-vims-autoread-work/53860166#53860166) & [Can vim monitor realtime changes to a file - Stack Overflow](https://stackoverflow.com/questions/2157914/can-vim-monitor-realtime-changes-to-a-file) – user202729 Dec 09 '21 at 12:03

3 Answers3

533

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 modifications).

The command :edit can be abbreviated by :e. The force-edit can thus be done by :e!

Karel
  • 1,438
  • 3
  • 13
  • 22
Thushi
  • 9,388
  • 4
  • 29
  • 43
212

In addition to manually refreshing the file with :edit, you can put into your ~/.vimrc

:set autoread

to make Vim automatically refresh any files that haven't been edited by Vim. Also see :checktime.

Sparhawk
  • 19,561
  • 18
  • 86
  • 152
Ingo Karkat
  • 11,664
  • 1
  • 34
  • 48
  • Actually the files are managed by Salt-stack, so I would know when the configuration file would change. But anyway, great tip! – twan163 Aug 08 '14 at 11:55
  • but it saying "No such file or directory" – LoveToCode Mar 21 '16 at 05:24
  • 2
    Buyer beware -- If you're working on fresh code and `git pull` be aware you could lose your unsaved changes on screen rather unintentionally. – 4Z4T4R Jun 16 '16 at 17:36
  • 5
    @toszter No, Vim will only refresh _unchanged_ buffers. In case of changes, there will still be a query: Keep, or load? – Ingo Karkat Jun 17 '16 at 07:28
  • Thanks! This solution is better because it does not break the folding feature as in `:edit`. – iamaziz Jun 21 '16 at 19:43
  • 11
    N.B. autoread [doesn't exactly work](https://stackoverflow.com/questions/2490227/how-does-vims-autoread-work) automatically. You either have to use `gvim`, or run external commands. – Sparhawk Sep 30 '16 at 01:37
  • 3
    Those using vim inside of tmux can get focus events by using https://github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call `:checktime` – Karl Bartel Jan 03 '17 at 15:50
  • 3
    `autoread` can be auto-triggered X seconds after the cursor stops moving, see [this answer](https://unix.stackexchange.com/a/383044/143394). – Tom Hale Aug 01 '17 at 06:16
114

TL;DR

Skip to the Wrap-up heading for the vimrc lines to add to do make your life better.

Manually

Run :checktime

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.

Automatically

To do automatically load changes, add in your vimrc:

set 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. 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 !ls or !sh etc)

Read on for solutions.

Trigger when cursor stops moving

Add to your vimrc:

au CursorHold,CursorHoldI * checktime

By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.

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

To have FocusGained (see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin: vim-tmux-focus-events

On tmux versions > 1.9, you'll need to add in .tmux.conf:

set -g focus-events on

Wrap-up

Notifications when autoread triggers are also possible.

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() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | 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

Thanks to ErichBSchulz for pointing me in the right direction with au CursorHold.

Thanks to this answer for solving the cmdwin issue.

Tom Hale
  • 28,728
  • 32
  • 139
  • 229