Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #248 - Auto-save current buffer periodically

Created: May 17, 2002 7:00 Complexity: intermediate Author: salmanhalim--AT--hotmail.com Version: 6.0 Karma: 14/9 Imported from: Tip#248

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.

Comments

This is cool. However I personaly like how swap files do this for me. By default a swap file is writen to after either 200 characters are typed or 4 seconds of inactivity has elapsed. Since a swap file writes faster and updates sooner a recovery is quite simple and sometimes more reliable if something goes wrong.

This is just my personal preference. Some may like the physical auto saving as opposed to swap file recovery. Word for example uses both methods by default. (Yes, I know bad example! Please no flames. ^_^)

vim at tritarget.com , May 20, 2002 23:08


Actually, it wasn't my intention to provide this as a mechanism without knowing about the swap file -- am fully aware of how that works. However, someone had asked about this on the Vim email list and since I posted a longish answer there, I decided to make a tip out of it also.

salmanhalim--AT--hotmail.com , May 22, 2002 5:55


Advertisement