124

The command to refresh a file from version on disk is :e!

How can I do the same for all files in the buffer?

Background: I need that because I am using git with multiple branches with one vim open that contains a buffer. When I checkout a branch, I would like to have vim refresh.

3cheesewheel
  • 9,133
  • 9
  • 39
  • 59
odwl
  • 2,095
  • 2
  • 17
  • 15

5 Answers5

124

The :checkt[ime] command is designed for this very purpose.

It will prompt you to reload any buffers that have changed; if you wish to skip the prompt, you can do :set autoread beforehand (you'll still get a prompt on buffers with local unsaved changes).

It also avoids the syntax highlighting issue mentioned by Steven Lu on the accepted answer; :bufdo turns off syntax highlighting by design.

Found via: http://vim.1045645.n5.nabble.com/Bug-report-bufdo-e-breaking-syntax-highlighting-on-displayed-buffers-tp1209995p1209998.html

Community
  • 1
  • 1
Paul Fenney
  • 1,656
  • 1
  • 14
  • 17
86

Read the documentation for bufdo, it should do what you want.

pmf
  • 7,619
  • 4
  • 47
  • 77
  • 35
    ok great so, :bufdo e! will do that. But there is a prompt Load (Y/N) each time. How can I get rid of it? – odwl Aug 13 '09 at 13:44
  • 22
    Use `set noconfirm` before using the `bufdo` command (`set confirm` afterwards to reanable). – pmf Aug 13 '09 at 13:59
  • 1
    I would use `bufdo e` (no exclamation mark) instead of `bufdo e!` as it can detect unsaved changes where there should be none, just to be on the safe side – Watcom Mar 07 '13 at 17:36
  • 23
    `bufdo e` curiously leaves all the buffers un-syntax-highlighted – Steven Lu Jul 09 '13 at 23:03
  • 4
    @StevenLu Check PaulFenney's answer below. – DBedrenko Feb 21 '14 at 11:40
  • 14
    Why are people voting this up? It's not only not an answer, but the direction it leads you means your syntax highlighting gets disabled after reload. – Dmitry Minkovsky Nov 12 '15 at 15:19
  • 3
    run `:syntax on` to re-enable syntax highlighting – Zane Aug 16 '16 at 15:33
  • 1
    Downvoting, because @paulfenney answer is more appropriate. – Igor Stoppa May 09 '18 at 12:32
  • @dmitry-minkovsky Because this is the only correct answer to the question asked. And that is useful in a broader context then just files updated in the background. – Slava Nov 08 '21 at 11:04
19

Here's what I ended up putting in my .vimrc:

fun! PullAndRefresh()
  set noconfirm
  !git pull
  bufdo e!
  set confirm
endfun

nmap <leader>gr call PullAndRefresh()
Ivan -Oats- Storck
  • 4,096
  • 3
  • 30
  • 39
7

From :help 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.

If, like me, you just want to always passively reload stale-but-unmodified buffers, then this seems like it should get the job done.

However the final detail is when vim notices the stale buffer. That can be forced with checktime. If you have focus events set up, then we can run checktime whenever we gain focus like so:

set autoread
autocmd FocusGained * checktime

This answer also has some interesting details.

Community
  • 1
  • 1
phs
  • 10,687
  • 4
  • 58
  • 84
1

As @Matthew S Mentioned here https://vi.stackexchange.com/a/462, you can use:

:set noconfirm
:bufdo !e
:set confirm
MOHRE
  • 1,096
  • 4
  • 15
  • 28