Tip 595 Printable Monobook Previous Next

created 2003 · complexity intermediate · author Adam Wolff · version 5.7

I generally liked the warnings that Vim gives when a file changes outside of the editor. However, there are times when I want to run a shell command that changes the buffer, and I don't want to hear about it.

I've come up with the following convoluted method to do this, but if there's a better way, I'd love to know.

function ChangeThisBuffer
  "set an environment variable to current buffer name
  let $aucfile = expand( "%" )
  "add autocmd which only applies to this buffer which removes itself once it runs once
  autocmd FileChangedShell $aucfile autocmd! FileChangedShell $aucfile
  execute( 'silent !mycommand' )

One problem with this approach is that if the shell command fails, or doesn't really change the file, then you won't be notified the next time it changes.

Comments[edit | edit source]

Untested, but this sounds like it would work/is what you are after.

function ChangeThisBuffer
  setlocal autoread
  execute( 'silent !mycommand' )
  set autoread<

To avoid the problem with the autocommand not being removed, you could explicity remove it in the function instead of relying on the autocommand itself to do so. Here's what I have in some of my functions:

exe "au FileChangedShell " . expand("%") . " let &cpo = &cpo"
" Do stuff to the file.
exe "au! FileChangedShell " . expand("%")

The "let &cpo = &cpo" command is just a convenient no-op.

Community content is available under CC-BY-SA unless otherwise noted.