Vim Tips Wiki
(Move categories to tip template)
(Adjust previous/next navigation)
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
 
|id=595
 
|id=595
 
|previous=593
 
|previous=593
|next=596
+
|next=597
|created=October 29, 2003
+
|created=2003
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Adam Wolff
 
|author=Adam Wolff
Line 26: Line 26:
 
</pre>
 
</pre>
   
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 &lt;b&gt;next&lt;/b&gt; time it changes.
+
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 <b>next</b> time it changes.
   
 
==Comments==
 
==Comments==
Line 36: Line 36:
 
setlocal autoread
 
setlocal autoread
 
execute( 'silent !mycommand' )
 
execute( 'silent !mycommand' )
set autoread&lt;
+
set autoread<
 
endfunction
 
endfunction
 
</pre>
 
</pre>
Line 45: Line 45:
 
<pre>
 
<pre>
 
checktime
 
checktime
exe "au FileChangedShell " . expand("%") . " let &amp;cpo = &amp;cpo"
+
exe "au FileChangedShell " . expand("%") . " let &cpo = &cpo"
 
" Do stuff to the file.
 
" Do stuff to the file.
 
checktime
 
checktime
Line 51: Line 51:
 
</pre>
 
</pre>
   
The "let &amp;cpo = &amp;cpo" command is just a convenient no-op.
+
The "let &cpo = &cpo" command is just a convenient no-op.
   
 
----
 
----

Latest revision as of 10:03, 2 December 2008

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' )
endfunction

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[]

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

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

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:

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

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