Vim:每次按回写我的文件 [英] Vim: write back my file on each key press
问题描述
我希望vim尽可能自动地自动写入文件.理想的是每次击键.
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 will see it. It's a makefile for a latex document, and I'd like the previewer to show me a nearly up-to-date document when I'm finished typing.
" Choose your own statusline here
let g:pbstatusline="%F\ %y\ %l:%c\ %m"
set statusline=%F\ %y\ %l:%c\ %m
autocmd FileType tex setlocal autowriteall
" Save the file every 5 keypresses
autocmd FileType tex setlocal statusline=%!pb:WriteFileViaStatusLine()
" Save the file every time this event fires.
autocmd FileType tex :autocmd InsertLeave,CursorHold,CursorHoldI * call pb:WriteFileViaStatusLine("always")
" 1 optional param: "always" is only allowed value.
let s:writefilecounter = 0
function! pb:WriteFileViaStatusLine(...)
if s:writefilecounter > 5 || (a:0 > 0 && a:1 == "always")
if &buftype == ""
write
endif
let s:writefilecounter = 0
else
let s:writefilecounter = s:writefilecounter + 1
endif
return g:pbstatusline
endfunction
推荐答案
一种方法是使用您的状态行:
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
只要状态行可见,它就会在每次更改文件后更新.
更新后,将调用WriteFile()
函数,该函数将写入文件(并在默认状态行返回我的近似值).使用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.
这篇关于Vim:每次按回写我的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!