Tip #248: Auto-save the current buffer periodically.
tip karma |
Rating 9/7, Viewed by 554
|
created: |
|
May 17, 2002 7:00 |
|
complexity: |
|
intermediate |
author: |
|
[email protected] |
|
as of Vim: |
|
6.0 |
I have no idea if this was implemented in vim 5.3 or not, but you can
definitely do the following kludge in 6.x by using CursorHold and
localtime:
- When you start reading a file, set a buffer variable to the current
time:
au BufRead,BufNewFile * let b:start_time=localtime()
- Set a CursorHold event to check to see if enough time has elapsed
since the last save and save if not:
au CursorHold * call UpdateFile()
- Define a function to save the file if needed:
" only write if needed and update the start time after the save
function! UpdateFile()
if ((localtime() - b:start_time) >= 60)
update
let b:start_time=localtime()
else
echo "Only " . (localtime() - b:start_time) . " seconds have elapsed so far."
endif
endfunction
- Reset the start time explicitly after each save.
au BufWritePre * let b:start_time=localtime()
Obviously, you should get rid of the else portion once you're certain
that this does indeed do what you wanted.
The thing to note is that the CursorHold will only fire after
'updatetime' milliseconds of inactivity have elapsed. So, if you type
rapidly for one and a half minutes non-stop, it won't actually save
anything until you STOP activity long enough. This may be what you want
anyway because it won't interrupt your activity with a forced save.
The actual save-delay can be changed from '60' to another number (in seconds) or a variable or anything like that. This entire functionality can be easily wrapped inside a nice script which enables/disables this on a per-buffer basis (maybe with maps etc.). If desired, I can provide that also.
<<Preexisting code indentation |
C/C++: Quickly insert #if 0 - #endif around block of code >>
Additional Notes
|