使用 Vim 的持续撤销?

Vim 7.3中的一个新特性是“持久撤销”,它允许撤销树在退出缓冲区时保存到一个文件中。

不幸的是,我还没有完全能够得到它正确启用,或者我一定是使用它错误。以下是我目前为止的尝试:

我将以下内容添加到 ~/. vimrc

set undofile                " Save undos after file closes
set undodir=$HOME/.vim/undo " where to save undo histories
set undolevels=1000         " How many undos
set undoreload=10000        " number of lines to save for undo

在这之后,我应该能够打开任何文件,编辑它,然后保存-关闭它,当我再次打开它时,我应该能够撤销/重做,就好像我从未离开过。不幸的是,情况似乎并非如此,因为从来没有写过撤销文件。

备注:

  1. 我在 Win 7上使用了 Vim 7.3,它来自于没有霜的 Vim 项目。

  2. $HOME/. vim/undo 在我的文件系统中存在

40093 次浏览

I suppose $HOME doesn't work as advertised.

On my system, :echo $HOME shows H:\, but : e $HOME/ says: ~/ invalid filename.

You could try with an absolute path to see whether it cures it

I tried this in my _gvimrc:

" Persistent undo
try
set undodir=C:\vim\undodir
set undofile
catch
endtry

It started working as advertised when I deleted the try-catch bracket, thus:

" Persistent undo
set undodir=C:\vim\undodir
set undofile

I had to create the directory.

Put this in your .vimrc to create an undodir if it doesn't exist and enable persistent undo. Tested on both Windows and Linux.

" Put plugins and dictionaries in this dir (also on Windows)
let vimDir = '$HOME/.vim'


if stridx(&runtimepath, expand(vimDir)) == -1
" vimDir is not on runtimepath, add it
let &runtimepath.=','.vimDir
endif


" Keep undo history across sessions by storing it in a file
if has('persistent_undo')
let myUndoDir = expand(vimDir . '/undodir')
" Create dirs
call system('mkdir ' . vimDir)
call system('mkdir ' . myUndoDir)
let &undodir = myUndoDir
set undofile
endif

This now works as expected: file.txt open in a Vim 7.4 buffer on Windows 7, :setlocal undofile, then save a change to the buffer, and the undofile .file.txt.un~ is created alongside because :set undodir? reports that "undodir=." by default - ie no need to specify this manually. You can also :set undofile in a modeline.