I\'d like vim to automatically write my file as often as possible. The ideal would be every keystroke.
I need to save regularly so that my background build process wi
One hack is to use your status line:
function! WriteFile()
if &buftype == ""
write
endif
return '%f %h%w%m%r%=%-14(%l,%c%V%) %(%P%)'
endfunction
setlocal statusline=%!WriteFile()
set laststatus=2
As long as the status line is visible, it's updated after each change to the file.
When updated, the WriteFile()
function is called, which writes the file (and returns my approximation at the default status line). With laststatus=2
, the status line is shown even when only one window is open.
This will keep the current buffer saved after each change.
If you're running Linux, you can take a look at
http://www.charlietanksley.net/philtex/vim-live-latex-preview/
He's using scripts and MuPDF(a lightweight pdf viewer) for live preview of latex markup.
It's very fast and the best/easiest solution I have found so far.
I've been using it for quite some time now and it works great!
There are CursorMoved and CursorMovedI autocmd events, but I don't think there's one that applies every single time you type in Insert mode.
You could also, were you so bold, rebind every single printable character in Insert mode to save and then type the character.
Whoops! I have been informed I forgot to mention that this usage is in the TODO list, and is "Coming Soon®". I actually wanted this feature a few days ago and discovered it doesn't work yet. Drat!
use autosave option in .vimrc (_vimrc for windows)
set autosave=4
This will save your file 4 seconds after the last change. Setting it to one would accomplish what you're looking for. It'll be automatic and always work. (Simpler is better)
TODO ... evidently I wasn't retaining WHERE I found this when I wrote this answer long ago. Thanks @sehe
These hacks are not needed any more. Vim can automatically write a file to disk whenever it is changed. Just add this to your $MYVIMRC:
autocmd TextChanged,TextChangedI <buffer> write
I believe you need Vim 7.4. In contrast to autosave=1
, this will save your file as soon as you change it.
I suggest using the method I described in similar Save file after each edit in vim question:
The CursorHold
and CursorHoldI
might help. According to docs:
|CursorHold| the user doesn't press a key for a while
|CursorHoldI| the user doesn't press a key for a while in Insert mode
Those events fire only once after inactivity and depend on updatetime
variable (default: 4000ms). So you can:
:au CursorHold <buffer> :update
Which will update current buffer file (i.e. save only if modified) after default 4 seconds of inactivity in Normal mode.
Add autocommand for CursorHoldI
if you want to get the same behavior in Insert mode.
Setting CursorHold
, CursorHoldI
events along with very short updatetime
will make vim save the file instantly after the edit.