1

I use an autocmd to copy a directory using rsync when saving a file with BufWritePost. When I save multiple files at once (using :wa), it runs rsync after every buffer save.

How can I run rsync or the autocmd only after all files have been written?

4
  • 1
    The only reasonable thing I can think of is something like VimLeave or other exiting commands, which is not really what you asked for.
    – D. Ben Knoble
    Jan 5, 2022 at 14:25
  • Welcome to Vi and Vim!
    – filbranden
    Jan 5, 2022 at 16:09
  • @D.BenKnoble Thanks for the response! I tried making the autocmd conditional on the number of buffers to be written, but I couldn't find a way to do that. Do you know how to get the number of unsaved buffers?
    – Ruben
    Jan 6, 2022 at 11:04
  • 1
    Check how many are &modified, maybe?
    – D. Ben Knoble
    Jan 6, 2022 at 12:24

1 Answer 1

2

In the end I managed to do this by checking the number of modified buffers. Once the number of buffers reaches 0, only then run rsync.

func! CountModifiedBuffer()
  " See https://vi.stackexchange.com/a/14313/40380
  return len(filter(getbufinfo(), 'v:val.changed == 1'))
endfunc

augroup project
  autocmd!
  autocmd BufWritePost path/to/project/*
        \ if CountModifiedBuffer() == 0
        \ | execute "!rsync -avu src dest | grep -v .git/"
        \ | endif
augroup END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.