60

Assuming that a file doesn't have unsaved changes in the VIM buffer, I'd like it to reload automatically if the file changes on disk. The most frequent use case for this is when I update the working copy in which the file resides.

How can I achieve this?

2
  • 1
    I think this is what you mean, but my specific use case is when I check out a different git branch or commit, I want vim to reload the file. Commented Feb 12, 2016 at 18:56
  • I'm going to reference the answer which worked for me (terminal vim) perfectly stackoverflow.com/a/53860166/4814774
    – daGo
    Commented Mar 31, 2020 at 6:27

8 Answers 8

40

In your ~/.vimrc:

set autoread
2
  • 14
    Generally autoread will only trigger after executing an external command. This answer details the solutions.
    – Tom Hale
    Commented Aug 1, 2017 at 5:12
  • 1
    This doesn't work Follow the @TomHale link for a detailed answer that does
    – JonnyRaa
    Commented Nov 13, 2017 at 10:35
27

This is what worked for me

set autoread                                                                                                                                                                                    
au CursorHold * checktime  

The first line wasn't enough by itself. You can put in you .vimrc

credit to Phan Hai Quang

5
  • The autocmd shouldn't be necessary--checktime is supposed to run internally at the same time the CursorHold event is triggered. In other words, you're just making Vim do a second checktime.
    – Heptite
    Commented Jun 18, 2016 at 16:37
  • For me it is necessary. Best solution I've found after lots of research. @Heptite, do you have chapter and verse for your assertion?
    – Tom Hale
    Commented Aug 1, 2017 at 5:19
  • 1
    @TomHale I've reviewed the Vim docs and it doesn't explicitly say, as far as I can tell, but ":help timestamp" would seem to indicate I'm the one in error, and that Vim only performs the check on specific events, so the autocmd could be necessary to cause it to check more frequently.
    – Heptite
    Commented Aug 1, 2017 at 16:17
  • not working on CentosOS 7 & vim 7.4
    – Jake
    Commented May 24, 2021 at 2:05
  • On macvim 9 on mac os ventura, with these commands, the buffer doesn't auto-fresh instantly - it auto-refreshes after around 10 seconds to 30 seconds.
    – user674669
    Commented Aug 9, 2023 at 8:58
11

from this answer (refering to an answer by PhanHaiQuang and a comment by @flukus)

One can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)

:set autoread | au CursorHold * checktime | call feedkeys("lh")

Explanation:
- autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
- CursorHold * checktime: when the cursor isn't moved by the user for the time specified in 'updatetime' (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
- call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)

5
  • 1
    lh does not do nothing, if the cursor is at the last position on the line, lh will change its location. Commented Apr 25, 2018 at 23:41
  • Exactly, thats the point. The file is reloaded with the minimum of interaction or changes to the file. You could jump to the last or first line if you want (just replace lh with gg or G), but the goal here, is just to reload the file in vim, and thats whats done by this one-liner, nothing more. What Do you like to be done here?
    – MacMartin
    Commented Apr 26, 2018 at 9:01
  • @MahmoudAl-Qudsi ... meaning, that "lh" just triggers the CursorHold - action (I mentioned that in the Explanation), which then again triggers the autoread
    – MacMartin
    Commented Apr 26, 2018 at 9:42
  • @eli, your solution works like a charm. But I noticed that when you have ctrlp plugin, because of your solution it keeps entering "lh" chars each 4 second in the search field. Do you know how to limit that? Commented May 2, 2020 at 17:59
  • I don't know for sure, I don't use ctrlp. But if the above command is running and it has focus on a searchfield, then it will insert the characters. It's intended, that the the command is run inside the file, that you are "watching". But if hitting a key triggers some ctrlp-mechanism, then this is bad. Maybe you can define what triggers the search and then use other characters for the feedkey function (the only purpose of "lh" is to move the cursor in some way)
    – MacMartin
    Commented May 3, 2020 at 13:17
6

Autoread does not work correctly. The following works for me:

You need to first install the script from here.

I got the best results by calling the setup function directly, like so.

let autoreadargs={'autoread':1} 
execute WatchForChanges("*",autoreadargs) 

The reason for this, is that I want to run a ipython/screen/vim setup.

You can easily convert this into an enhanced version of view.

script the process..

mkdir -p ~/bin
cat <<`OUT` > ~/bin/vimviewer
#!/usr/bin/env sh
exec vim -n --cmd "source /home/bryan/.vim/.vimrc.watchforchanges | let autoreadargs={'autoread':1} | execute WatchForChanges('*',autoreadargs)" $@
`OUT`

chmod 755 ~/bin/vimviewer
vimview test.txt
1
  • 1
    If you're using one of the vim GUIs such as gVim or MacVim then the autoread command integrates fine with the rest of the environment. E.g. editing the file in some other program, another editor or from source control, then switching back to vim will autoread the updated files. The problem seems to be with how the terminal version of vim integrates. The terminal version can't tell when you switch between programs either in the GUI or using terminal multiplexers like tmux, so it fails to autoread at relevant times. It would be nice if vim would just subscribe to relevant file-system events.
    – bames53
    Commented May 3, 2014 at 23:31
3

vim-autoread plugin has worked for me so far:

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.

3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Burgi
    Commented May 5, 2017 at 13:34
  • @Burgi I've added plugin description that explains how it solves the problem. Commented May 5, 2017 at 14:01
  • I tried it but I have to type a navigation command (like j) to see the effective update. I created an issue here about this: github.com/djoshea/vim-autoread/issues/3 Commented Dec 19, 2018 at 13:34
1

There are some plugins listed here that might work depending on your version of Vim.

Another approach is to periodically send the checktime command to vim from an external process. Here's a ShellJS script to do this (requires shelljs-plugin-sleep, NeoVim, and nvr)

while (true) {
  sleep(15);
  var servernames = exec('nvr --serverlist').trim().split('\n');
  if (!servernames.length || !servernames[0]) {
    continue;
  }
  servernames.forEach(
    (sn) => exec(`nvr -c "checktime" --servername ${sn}`) );
}
1
1

Using it with an alias, which I place on /etc/profile for use on all users:

alias viml="vim $\"+:set autoread | au CursorHold * checktime | call feedkeys('lh')\" "

Then you can call viml, wich will reload the file on every change (with an small lag). It will keep you on the current cursor line. Press Shift+G to go to the end of the file.

sudo viml /var/log/mysql/mysql.log
0

I use :set autoread, not in .vimrc but in the command line for the file concerned, e.g.

vim filename +'set autoread'

Works fine. Only drawback is I do have to focus vim and press G to show latest additions to the file.

You must log in to answer this question.

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