I guess your motivation of clearing the undo information on writes is to avoid going beyond the saved buffer state when undoing many steps (uuuu...
). I personally use a different approach: I've tweaked the u
command to stop and beep once when reaching the saved buffer state. I need to u
once more to go beyond.
My customization integrates with the repeat.vim plugin; It also requires some utility functions from my ingo-library plugin (it's now also available on GitHub).
runtime autoload/repeat.vim " Must load the plugin now so that the plugin's mappings can be overridden.
let s:undoPosition = []
function! s:StopAtSavedPosition( action )
" Buffers of type "nofile" and "nowrite" never are 'modified', so only do
" the check for normal buffers representing files. (Otherwise, the warning
" annoyingly happens on every undo.)
if ingo#buffer#IsPersisted() && ! &l:modified && s:undoPosition != ingo#record#PositionAndLocation(1)
" We've reached the undo position where the buffer contents correspond
" to the persisted file. Stop and beep, and only continue when undo is
" pressed again at the same position.
call ingo#msg#WarningMsg(a:action . ' reached saved buffer state')
execute "normal! \<C-\>\<C-n>\<Esc>" | " Beep.
let s:undoPosition = ingo#record#PositionAndLocation(1)
return 1
else
let s:undoPosition = []
return 0
endif
endfunction
nnoremap <silent> u :<C-U>if ! &l:modifiable<Bar>execute 'normal! u'<Bar>elseif ! <SID>StopAtSavedPosition('Undo')<Bar>call repeat#wrap('u',v:count)<Bar>endif<CR>
nnoremap <silent> <C-R> :<C-U>if ! &l:modifiable<Bar>execute "normal! \<lt>C-R>"<Bar>elseif ! <SID>StopAtSavedPosition('Redo')<Bar>call repeat#wrap("\<Lt>C-R>",v:count)<Bar>endif<CR>
:edit
in order to reset undo information? You could follow:help clear-undo
to do this without:edit
; I would define a custom:Write
command that combines both.:edit
in order to reset undo information?" Yes, that's exactly what I'm doing. Thanks for the suggestion; I'll try rewriting the:we
command in my.vimrc
file to do that instead of:edit
. If you'd like to put that into an answer, I'll mark it as accepted.