1

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

keflavich
  • 18,278
  • 20
  • 86
  • 118
  • 1
    Using a proper VCS may provide you the safety net you are looking for. Or using Vim directly on the remote machine. – romainl Mar 26 '13 at 19:47
  • Using VCS directly via vim would be a great option. Using vim directly is not a real option, at least for me, as it prevents using gvim and adds lag to the editing process. – keflavich Mar 26 '13 at 21:28
  • GVim is not better than CLI Vim and yes, just put your stuff in a Git repo, clone it on your home computer, work, commit, push then pull when you are back at work. – romainl Mar 26 '13 at 21:35
  • While that is one valid workflow, it is not the question I'm asking here. Convenience is more important to me than the safety net provided by VCS, but I'm asking if there's a reasonable way to get both. – keflavich Mar 26 '13 at 21:37

2 Answers2

1

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
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • That's a clever solution, I like it. However, I think I'd prefer `edit` over `edit!` - I'd rather receive a warning if I've made both local and remote edits instead of just wiping the local edits. – keflavich Mar 27 '13 at 14:47
  • 1
    Unpersisted local changes are not overwritten; that's what the `! &modified` is for. With `:edit`, you'll receive a warning in that case; my solution remains silent. – Ingo Karkat Mar 27 '13 at 15:11
1

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. 
Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60