How can I discard my undo history in vim?

Solution 1:

:set undoreload=0 | edit

should do what you're asking for.

Solution 2:

The old :edit! behaviour in a one-liner:

:exe "set ul=-1 | e! | set ul=" . &ul

Solution 3:

Benoit's function didn't work for me, but I found something similar in the vim manual, here:

http://www.polarhome.com/vim/manual/v73/undo.html#undo-remarks

I slapped it into a function, added to my vimrc and it seems to be working fine on vim 7.3:

" A function to clear the undo history
function! <SID>ForgetUndo()
    let old_undolevels = &undolevels
    set undolevels=-1
    exe "normal a \<BS>\<Esc>"
    let &undolevels = old_undolevels
    unlet old_undolevels
endfunction
command -nargs=0 ClearUndo call <SID>ForgetUndo()

This can be used with :ClearUndo.

Solution 4:

Probably:

:let old_ul=&ul
:set ul=-1
:let &ul=old_ul
:unlet old_ul

('ul' is alias for 'undolevels').

Solution 5:

" Clear undo history (:w to clear the undo file if presented)
command! -bar UndoClear exe "set ul=-1 | m-1 | let &ul=" . &ul

  • When you set 'undolevels' to -1 the undo information is not immediately cleared, this happens at the next change, i.e. m-1, which doesn't actually change your text.
  • When 'undofile' is on, :w to clear the undo file, or chain like UndoClear|w.