autosave.vim

vim plugin to autosave buffer when leaving cursor idle
Index Commits Files Refs README LICENSE
plugin/autosave.vim (1009B)
   1 " default filetypes
   2 if !exists('g:auto_save_file_types')
   3     let g:auto_save_file_types = [ 'markdown', 'text' ]
   4 endif
   5 
   6 let s:auto_save_filetype_enabled = 0
   7 let s:auto_save_write = 0
   8 
   9 function s:auto_save()
  10     if s:auto_save_filetype_enabled == 1 || g:auto_save_all_filetypes == 1
  11         if s:auto_save_write == 1
  12             let s:auto_save_write = 0
  13             if &modified
  14                 let l:bytes = line2byte('$') + len(getline('$'))
  15                 if filereadable(expand('%:p')) == 1
  16                     silent! write
  17                     echo "\"" .. expand('%') .. "\" " .. line('$') .. "L, " ..
  18                                 \ l:bytes .. "B written [autosave]"
  19                 else
  20                     silent! write
  21                     echo "\"" .. expand('%') .. "\" [New] " .. line('$') ..
  22                                 \"L, " .. l:bytes .. "B written [autosave]"
  23                 endif
  24             endif
  25         endif
  26     endif
  27 endfunction
  28 
  29 au TextChanged,TextChangedI <buffer> silent! let s:auto_save_write = 1
  30 au CursorHold,CursorHoldI <buffer> call s:auto_save()
  31 
  32 au BufEnter,FileType * if index(g:auto_save_file_types, &filetype) !=# -1 |
  33             \ let s:auto_save_filetype_enabled = 1 | endif