title: How to write the contents of the current buffer back to file on each key press in Vim?
body: <p>I’d like Vim to automatically write my file as often as possible. The ideal would be every keystroke.</p>
  <p>I need to save regularly so that my background build process will see it. It’s a makefile for a LaTeX document, and I’d like the previewer to show me a nearly up-to-date document when I’m finished typing.</p>
  <h3>Eventual solution</h3>
  <p>The answers below helped to make this.</p>
  <pre><code>&quot; Choose your own statusline here
  let g:pbstatusline=&quot;%F\ %y\ %l:%c\ %m&quot;
  set statusline=%F\ %y\ %l:%c\ %m
  
  autocmd FileType tex setlocal autowriteall
  
  &quot; Save the file every 5 keypresses
  autocmd FileType tex setlocal statusline=%!pb:WriteFileViaStatusLine()
  
  &quot; Save the file every time this event fires.
  autocmd FileType tex :autocmd InsertLeave,CursorHold,CursorHoldI * call pb:WriteFileViaStatusLine(&quot;always&quot;)
  
  &quot; 1 optional param: &quot;always&quot; is only allowed value.
  let s:writefilecounter = 0
  function! pb:WriteFileViaStatusLine(...)
     if s:writefilecounter &gt; 5 || (a:0 &gt; 0 &amp;&amp; a:1 == &quot;always&quot;)
        if &amp;buftype == &quot;&quot;
           write
        endif
        let s:writefilecounter = 0
     else
        let s:writefilecounter = s:writefilecounter + 1
     endif
  
     return g:pbstatusline
  endfunction
  </code></pre>