如何在Vim中每次按键时将当前缓冲区的内容写回到文件中? [英] How to write the contents of the current buffer back to file on each key press in Vim?

查看:45
本文介绍了如何在Vim中每次按键时将当前缓冲区的内容写回到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Vim尽可能自动地自动写入文件.理想的是每次击键.

I’d like Vim to automatically write my file as often as possible. The ideal would be every keystroke.

我需要定期保存,以便我的后台构建过程能够看到它.这是LaTeX文档的makefile,我希望预览器在输入完毕后向我显示一个几乎最新的文档.

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.

以下答案有助于实现这一目标.

The answers below helped to make this.

" 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆