更新2015-06-28:我修复了一个小错误,并将其作为插件发布。插件代码稍好一点,因为它在移动光标后会再次发出警告;我建议您使用该插件。
superjer的答案很有效,但不幸的是,您只能撤消上一个Vim会话中的更改,而不能撤消所有先前的Vim会话中的更改。
这是因为wundo
覆盖撤消文件。它没有合并。据我所知,没有办法解决这个问题。
因此,这是我的替代解决方案,当您撤消撤消文件中的更改时,它将显示一条红色的大警告消息。
这类似于Ingo Karkat的答案,但是它不需要外部插件,并且有一些细微的差异(显示警告而不是发出哔声,不需要您再按u
两次)。
请注意,这只是修改u
和<C-r>
结合,并且不与U
,:undo
和:redo
命令。
" Use the undo file
set undofile
" When loading a file, store the curent undo sequence
augroup undo
autocmd!
autocmd BufReadPost,BufCreate,BufNewFile * let b:undo_saved = undotree()['seq_cur'] | let b:undo_warned = 0
augroup end
" Remap the keys
nnoremap u :call Undo()<Cr>u
nnoremap <C-r> <C-r>:call Redo()<Cr>
fun! Undo()
" Don't do anything if we can't modify the buffer or there's no filename
if !&l:modifiable || expand('%') == '' | return | endif
" Warn if the current undo sequence is lower (older) than whatever it was
" when opening the file
if !b:undo_warned && undotree()['seq_cur'] <= b:undo_saved
let b:undo_warned = 1
echohl ErrorMsg | echo 'WARNING! Using undofile!' | echohl None
sleep 1
endif
endfun
fun! Redo()
" Don't do anything if we can't modify the buffer or there's no filename
if !&l:modifiable || expand('%') == '' | return | endif
" Reset the warning flag
if &l:modifiable && b:undo_warned && undotree()['seq_cur'] >= b:undo_saved
let b:undo_warned = 0
endif
endfun