scp経由でファイルを編集している時の保存を非同期化する
.vimrc
に以下を記述
" inspired by https://github.com/skywind3000/asyncrun.vim/wiki/Get-netrw-using-asyncrun-to-save-remote-files
" required: skywind3000/asyncrun.vim
fu! s:AsyncSaveRemoteFile(...) abort
let l:tmpfile = tempname()
sil exe 'w! '.fnameescape(v:cmdarg).' '.fnameescape(tmpfile)
let curbufname = expand('%')
let scpurm = '^scp://\([^/#:]\+\)\%([#:]\(\d\+\)\)\=/\(.*\)$'
let machine = substitute(curbufname,scpurm,'\1','')
let port = substitute(curbufname,scpurm,'\2','')
let fname = substitute(curbufname,scpurm,'\3','')
if port != ''
let scpcmd = 'scp -P '.shellescape(port)
else
let scpcmd = 'scp'
endif
let escaped_tmpfile = shellescape(tmpfile, 1)
let escaped_fname = shellescape(machine.":".fname, 1)
let bufnr = expand('<abuf>')+ 0
call asyncrun#run(
\ '',
\ { 'post': 'call delete('.escaped_tmpfile.')|call setbufvar('.bufnr.',"&modified",0)|echo "Saved. '.curbufname.'"' },
\ scpcmd.' '.escaped_tmpfile.' '.escaped_fname)
endfu
com! -range=% -nargs=* AsyncSaveRemoteFile call s:AsyncSaveRemoteFile(<f-args>)
fu! SetupAsyncSaveRemoteFile()
" disable Netrw's BufWriteCmd
au! Network BufWriteCmd scp://*
aug SetAsyncSaveSCP
au!
au BufWriteCmd scp://* exe "sil doau BufWritePre ".fnameescape(expand("<amatch>"))|exe "AsyncSaveRemoteFile ".fnameescape(expand("<amatch>"))|exe "sil doau BufWritePost ".fnameescape(expand("<amatch>"))
aug END
endfu
aug AsyncSaveRemoteFile
au!
au VimEnter * call SetupAsyncSaveRemoteFile()
aug END