156

Quite often I am working with files that are version controlled, so it is possible for changes to be made to them outside of Vim.

What is a quick way to reload the file without having to close and reopen Vim? And is there a way to do this automatically?

  • " Compare buffer with saved version nnoremap gd :DiffSaved<CR> – Tom Hale Sep 2 '16 at 14:24
  • If you have installed vim-faq, you can get an offline answer there: :h vim-faq and search /reload. The hard to memorize tag is :h faq-5.8. – Hotschke Jul 23 at 7:44
183

The command you want is :e (short for :edit). If you use :edit! it will discard local changes and reload.

You can also have vim do this automatically with set autoread in your vimrc.

  • 5
    See this answer for caveats and solutions regarding autoread. – Tom Hale Aug 1 '17 at 3:58
  • 4
    To do this for all buffers, use :bufdo :e or :bufdo :e! – Aaron H. Nov 13 '18 at 21:50
  • 1
    @AaronH. thank you for that! i usually have hundreds of buffers open at a time and when switching branches, I usually just close down vim and restart to refresh all of the buffers with new contents. This will be a major timesaver! – g19fanatic Dec 13 '18 at 13:42
  • 1
    @AaronH. The second : is optional: :bufdo e will work the same. – JoL Apr 26 at 1:07
21

In many cases, set autoread will not work as this answer explains:

Autoread does not reload file unless you do something like run external command (like !ls or !sh etc)

Focus events will trigger autoread, but these are not captured unless using gvim. So, if running plain vim inside a terminal window or tmux, 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

For additional goodness, adding the following to your vimrc will trigger autoread when changing buffers inside while inside vim:

au FocusGained,BufEnter * :checktime
  • This has been annoying me for years. Thanks for a thorough solution! 💯😄 – Nathan Long Aug 7 '18 at 9:54
8

If you've ever run an external command from Vim that modifies the current buffer, you may have noticed that it prompts you to reload the file since changes have been made to disk. You can actually invoke this prompt using the :checktime command.

3

Nice plugin to improve autoread https://github.com/djoshea/vim-autoread

Automatically causes vim to reload files which have been written on disk but not modified in the buffer since the last write from vim. This enables a file open in vim to be edited using another application and saved. Upon returning to vim, as long as you haven't modified the file since the last change, the file will be automatically updated to reflect the changes made on disk, as though you had pressed :e manually.

  • Welcome to the Vi Stack Exchange. This is nearly a link-only answer. You can improve upon it by describing the plugin (e.g. what does it do? How would one use it to accomplish OP's goals?) – John O'M. May 12 '18 at 3:56
0

I had the problem of looking at contents of a file, update the file externally, display the changed file and have the option to edit it when needed. I solved it with the Vim server/remote command line options, inspired by this answer.


With the following commands a file can be reloaded via an external command. First, open the file in question with Vim, while starting a server instance.

vim --servername JABBERWOCKY /tmp/alice.log

Let's assume the file has been changed externally, e.g. by a echo "Curiouser and curiouser" >> /tmp/alice.log. When necessary, send a command to the server instance to reload the file. This command will finish quickly and not spawn a new editor.

vim --servername JABBERWOCKY --remote-send ":e<CR>"

If the opened file was changed in Vim and externally, there will be a conflict during reload (a warning message will appear). For that case a decision has to be made: Leave it with that and keep changes in Vim, or discard local changes and force a reload.


The command with --remote-send above requires manual reloading. However, that command could be tied to a simple loop, using Bash's for/while, or using a hacky one-liner in Linux/Unix:

watch -n 1 'vim --servername JABBERWOCKY --remote-send ":e<CR>"'

Although not tested, I'm certain that something like this could be realized in PowerShell, too.

On Linux/Unix an event-driven approach can be realized by listening to file system events using e.g. the inotify library, if your not using too crazy file systems. Personally I like using the tool entr a lot, which seems to be included in major Linux distributions. Then I get:

echo /tmp/alice.log | entr vim --servername JABBERWOCKY --remote-send ":e<CR>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.