436

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?

4

4 Answers 4

537

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!

6
  • 2
    The "!" was tripping me, but now it's clear.
    – twan163
    Commented Aug 8, 2014 at 11:41
  • 16
    Lose modifications, yes and no. Crucially, you can still undo the :e!.
    – gerrit
    Commented Sep 10, 2015 at 9:22
  • 6
    Also, if you want to reload all your buffers, run: :bufdo e
    – pkout
    Commented Nov 4, 2016 at 21:43
  • 1
    @gerrit: Lose modifications, Definitely yes :) But you can recover the lost modifications by undoing. :)
    – Thushi
    Commented Dec 30, 2016 at 4:24
  • :w|e write and edit (reload) :)
    – user86041
    Commented Apr 27, 2017 at 7:41
215

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.

8
  • 3
    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
    Commented Jun 16, 2016 at 17:36
  • 6
    @toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load? Commented Jun 17, 2016 at 7:28
  • 11
    N.B. autoread doesn't exactly work automatically. You either have to use gvim, or run external commands.
    – Sparhawk
    Commented Sep 30, 2016 at 1:37
  • 3
    Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call :checktime Commented Jan 3, 2017 at 15:50
  • 3
    autoread can be auto-triggered X seconds after the cursor stops moving, see this answer.
    – Tom Hale
    Commented Aug 1, 2017 at 6:16
115

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.

1
  • How do you STOP vim from automatically reloading the file every time it changes? It's doing it by default and I don't have a vimrc file, so it must have been recently added to the defaults
    – matteo
    Commented Mar 17 at 22:46
2

Here's a lua version for neovim:

-- 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
vim.api.nvim_create_autocmd({'FocusGained', 'BufEnter', 'CursorHold', 'CursorHoldI'}, {
  pattern = '*',
  command = "if mode() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif",
})

-- Notification after file change
-- https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
vim.api.nvim_create_autocmd({'FileChangedShellPost'}, {
  pattern = '*',
  command = "echohl WarningMsg | echo 'File changed on disk. Buffer reloaded.' | echohl None",
})

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .