1

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?

  • What is the goal? I understand you want to drop the undo history, but why do you want to lose this undo history? Is it an issue with navigating the undo history/tree? Do you simply want to spam 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-pattern – Peter Rincker Jul 30 '18 at 15:35
  • @PeterRincker: basically yes, I'd like to spam u, 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. – geza Jul 30 '18 at 15:45
  • In my opinion, this sounds like a good situation for source control so you can always roll back to a "good" version. You may also be intrested in using :earlier with the f option to go to an earlier file write. e.g. :earlier 1f. I would recommend reading :h undo.txt – Peter Rincker Jul 30 '18 at 15:50
  • 1
    @PeterRincker: Yes, source control is an option, but it is inconvenient. 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. – geza Jul 30 '18 at 16:00
  • @geza Take a look at the undotree plugin (I updated my answer with details). Could be useful to you. – B Layer Jul 31 '18 at 1:52
4

I 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).

  • Thanks for the answer! Previously, 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. – geza Jul 29 '18 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.