Rob
Rob

Reputation: 4141

vim autocommand event for write all buffers :wa

I'm currently using the following mapping to, essentially copy over any files written in my dev env to my local server via executing a script. It works fine for individual files. However, I have a habit of doing :wa to save all buffers open:

au BufWritePost /path/to/dev/* silent !$HOME/bin/somescript.sh %:p 

Any suggestions for how I could rewrite this to be a conditional like:

if one file
  exec script to copy just that file # like I already have
if :wa
  # here I'd probably exec a script to just copy recursively

EDIT

Possible solution per ZyX's solution:

au BufWritePost /Users/rlevin/programming/sugar/Mango/sidecar/* silent !$HOME/bin/sugarbuild.sh %:p

" If we do :wa<CR> we check if command type is ':' and if command itself was
" 'wa'. If so, we call the command WA which calls BuildSidecarIfInProject.
" This checks if we're actually within the project's directory
cnoreabbrev <expr> wa ((getcmdtype() is# ':' && getcmdline() is# 'wa')?('WA'):('wa'))
command! WA :call BuildSidecarIfInProject()
function! BuildSidecarIfInProject()
  if fnamemodify('.', ':p')[:44] is# '/Users/rlevin/programming/sugar/Mango/sidecar'
    exec ":!$HOME/bin/toffeebuild.sh"
  endif                                                                                                                                                                        
endfunction

Upvotes: 2

Views: 541

Answers (2)

David Pope
David Pope

Reputation: 6587

Some smart guy once said, "Premature optimization is the root of all evil." If you really need on-the-fly backup/deployment to your server, why don't you just run the recursive version every time, or possibly bound to a hotkey? I.e. don't treat the single-file case specially. For example, rsync is pretty good at avoiding unnecessary copying.

Upvotes: 2

ZyX
ZyX

Reputation: 53604

There is no way to determine number of files saved, but you can remap/abbreviate wa:

command WA # command that executes a script to just copy recursively
cnoreabbrev <expr> wa ((getcmdtype() is# ':' && getcmdline() is# 'wa')?('WA'):('wa'))

Upvotes: 2

Related Questions