Search code examples
vimnetrw

vim autoread + netrw to prevent accidental overwriting


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).


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