Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Move categories to tip template)
Line 8: Line 8:
 
|version=6.0
 
|version=6.0
 
|rating=14/9
 
|rating=14/9
  +
|category1=
  +
|category2=
 
}}
 
}}
 
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:
 
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:

Revision as of 00:56, 25 April 2008

Tip 248 Printable Monobook Previous Next

created May 17, 2002 · complexity intermediate · author salmanhalim · version 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.

Comments

This is cool. However I personally 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. ^_^)


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.