Question

Is there any equivalent to :autoread when using netrw to access remote files in vim?

I access files on my work machine remotely via netrw, then when I arrive at work modify the files there. However, if I go back to remote access, it's easy to forget to re-load the file if it's already open in a buffer - if it has been modified on the work machine, then, the next :w will overwrite any changes I made while at work. I'm looking for a 'safety net' to reduce the risk of data loss (luckily, I haven't lost any yet because of .swp files, but that's not a very reassuring net).

Was it helpful?

Solution

With 'autoread', Vim just has to check the file modification time. Since the netrw targets reside on a different system, that lookup will be most costly and you have to trigger it yourself. One idea is to perform that check on the FocusGained event, i.e. when you come back to your (home machine's) Vim. Since on Windows GVIM, the netrw access pops up a console window (triggering another FocusGained), and to avoid too frequent checks, let's limit the check to a certain time interval, e.g. at most every 5 minutes:

:autocmd FocusGained ftp://*,scp://* nested
\   if ! &modified && ! exists('b:lastchecktime') || localtime() - b:lastchecktime > 300 |
\       edit! |
\       let b:lastchecktime = localtime() |
\   endif

OTHER TIPS

Did you try :checktime ?

        Check if any buffers were changed outside of Vim.
        This checks and warns you if you would end up with two
        versions of a file.
        [...]
        Each loaded buffer is checked for its associated file
        being changed.  If the file was changed Vim will take
        action.  If there are no changes in the buffer and
        'autoread' is set, the buffer is reloaded.  Otherwise,
        you are offered the choice of reloading the file. 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top