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 likeUndoClear|w
.
Related videos on Youtube
Nathan Long
I code mostly in Ruby and Elixir. More about me at nathanmlong.com and Stackoverflow Careers.
Updated on September 17, 2022Comments
-
Nathan Long over 1 year
Undo is nice to have in Vim. But sometimes, at a known good point, I want to erase my undo history - to be able to use
u
to undo individual changes, but only back to a certain point. (For instance, this might be when I last committed.)One way to accomplish this would be to close and reopen the file - the undo history starts clean at that point. But that's a hassle.
In the past, I accomplished this with
:edit!
. But in Vim 7.3, that doesn't discard the undo history.Is there another way to do this, apart from closing the file?
-
Nathan Long over 13 years@akira - no, I want to be able to use
u
to undo individual changes, but not past the point where I last committed. Exactly as if, every time I committed, I closed the file and re-opened it. Which is what:edit!
used to do - it was like closing the file and reopening it. This doesn't HAVE to be tied to a commit point, that's just the time when I'd most frequently want it. -
akira over 13 yearsah, that makes the problem much clearer. you should change your question a bit.
-
-
Nathan Long over 13 yearsThis works! Now I'm trying to make a single command do that - preferably
:edit!
. As a start, I've made a function called ClearUndos() with the commands you listed, but calling it doesn't seem to do anything, whereas doing the commands individually does... -
Benoit over 13 years
:command -nargs=0 Reset let old_ul=&ul | set ul=-1 | e! | let &ul = old_ul | unlet old_ul
-
Nathan Long over 13 yearsis that meant to be the contents of the function? It's not working for me, but maybe I misunderstood. Also - maybe move this to its own answer?
-
Nathan Long over 13 yearsYour function definition line needs () at the end.
-
Nathan Long almost 12 yearsThat does work. Would you mind explaining it?
-
Aldrik almost 12 yearsConcatenate the value of the undolevels setting on the end of the string of commands (so that it can be restored) and executes them. For more info see the documentation:
:h :exe
,:h :bar
,:h :set-args
,:h 'ul'
and:h :edit!
. -
Nathan Long over 11 yearsHa! Just turn that feature off, eh? :) Not sure why it took me so long to notice this.
-
PlasmaHH over 10 yearsWorking normally with Gundo plugin and undo file reloading, this thing comes really handy for me. However, I changed two things: instead of the normal edit command (which itself leaves some edit history on 7.4 it seems), I simply use an edit! (and always write before erasing the undo history). It seems that some internal lists are not properly updated, so if you want to use this with Gundo plugin, doing an additional edit! at the functions end will prevent it from erroring out later.
-
Ben about 10 years@PlasmaHH Could you please post the function for clearing undo history, because I did not understand what did you change... (you could maybe post it as an answer for those of us who use gundu). Thanks