Can vim monitor realtime changes to a file

My question is similar to this how to monitor a text file in realtime but I want to do it in vim. I know I can read an opened file use tail -f sample.xml file, and when new content is written to the file, it'll also write the new content to my screen. Can I have vim automatically fill the new data when a file is updated?


You can :set autoread so that vim reads the file when it changes. However (depending on your platform), you have to give it focus.

From the help:

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.


Don't know about automatically, but you can type:

:e!

to reload the file


Put the following in your .vimrc:

" check one time after 4s of inactivity in normal mode
set autoread                                                                                                                                                                                    
au CursorHold * checktime                                                                                                                                                                       

like @flukus said in a comment to a previous answer you can call feedkeys["lh"] (it moves the cursor to the right and back to the left, which normaly doesn't do harm when viewing a log file)

So, if you combine the rest of the answer you have a oneliner you can run from ex (whithin vim) when needed:

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


***(if you would want to jump (nearly) to the end of the file, just use "G" instead of "lh" with feedkeys)***

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)

Additionally you can :set syntax=logtalk to color the log

To stop the scrolling when using call feedkeys("G"), execute :set noautoread - now vim will tell, that the file was change ans ask if one wants to read the changes or not)

(Does this have any sideeffects?)

Edit: I see one side-effect: if one uses "G" as the feedkey, it will scroll down every currently opened buffer?! So, it's not possible to work in the left buffer of a splittet window while having the right buffer scroll down a logfile automatically