Previously (as far as I remember), e!
used to drop undo history. Now, I'm using vim 8.0, and e!
doesn't drop it.
How to drop undo history in vim 8.0?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI don't recall anything like that with e!
. At least not since 'persistent undo' was added back in 2010 as part of v7.3(b?). (I confirmed that the command sequence discussed below was in the documentation from that point forward.) I think when the feature was first added a lot of people wouldn't have had it enabled (i.e. :set undofile
). In that case e!
would certainly wipe undo entries recorded since the last save. Perhaps this is what you are recalling.
FYI, the way it works (with persistent undo enabled) is that a new "undo block" is added that reverses changes made since the last save. If you hit undo after e!
the state will be back to where it was immediately before e!
.
Anyways, you can clear undo history, including the persistent copy kept in a buffer's undo file, with the command sequence provided by :h clear-undo
...
:let old_undolevels = &undolevels
:set undolevels=-1
:exe "normal a \<BS>\<Esc>"
:let &undolevels = old_undolevels
:unlet old_undolevels
This sets 'undolevels'
to -1, indicating there should be 0 undo levels, and does a non-visible "dummy" operation on the buffer to force immediate overwriting of the history. (The next time the history is written to disk the undo file will be cleared, too.) Then the previous 'undolevels'
setting is restored.
Obviously if you intend to use this on a recurring basis you'll want to put it in a function/mapping or some such.
Side note: Vim doesn't provide a way to delete undo files. You'd have to do that manually. The file name for current buffer can be seen with :echo undofile('.')
.
Update: You might want to look at the undotree plugin. Besides allowing easy navigation of the undo tree it has a clear history function (which I just noticed despite having used the plugin for a long time).
e!
was reloaded the buffer, and the undo history was cleared (I'm not 100% sure about this, but I remember that vim worked like this) . Currently, e!
is considered just a command, which can be undone (previously, e!
cannot be undone). Or something like that.
Here is a vimscript function that shells out to forcefully delete the directory used to store undo files. This removes all undo history for all files.
function! ClearUndoHistory()
echom 'Deleting undodir=' .&undodir
system('rm -rf ' . &undodir)
endfunction
Note this function is not super robust; the help file for 'undodir' says that &undodir
can be a comma-separated list of directories. The case of multiple undodirs is not handled by the above.
It might also be wise to guard against removing &undodir in the case where e.g. &undodir=="."
.
u
until you get to a certain point? Are you working on some sort of plugin where this would be the correct behavior? With out more information, i feel like a bit of an anti-patternu
, until I got to a certain point. I have some files, which I constantly edit (I even have the editor open for months). And I know at certain points that the file is OK. At this point, I'd like to drop undo history. Timestamps are not good for me, because I may not remember the exact date, when the file was OK. With previous versions of vim,e!
worked, and it was an easy solution.:earlier
with thef
option to go to an earlier file write. e.g.:earlier 1f
. I would recommend reading:h undo.txt
e!
was 3-4 keystrokes in the editor, and it worked. I had been using this technique for several years without any problems (I only use this technique when the data is not that important, of course). The other use case for dropping undo history is memory consumption: sometimes I edit huge files (more than 1 GB), and it would be good if at certain points I could drop history, to free up some memory.