Auto-reloading a file in VIM as soon as it changes on disk

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

7 Answers

In your ~/.vimrc:

set autoread
2

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

4

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

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

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

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}`) );
}

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like