I have a "weird" usage pattern in vim that I often use - or have to use, depending on how you view it.

What normally happens is this: I enter some text, do some editing, then remove a bit of text again. Some time in the future, I figure that this deleted bit of text would again be useful, so the following happens:

  1. Undo until the deleted text appears again
  2. Yank the text into some register
  3. Redo the undo history until I'm at the "current" version again
  4. Paste the yanked text

What I'm wondering is this: Is there a way to search for something in the undo history of a file, so I won't have to undo dozens (or hundreds) of undoes to get to the right version?

Note that I'm explicitly not looking for an "external" VCS like Git or Subversion - I'm trying to search for changes before even having to save a file to disk.

  • 1
    I believe there is a "market" for this: to accelerate the workflow of finding a past change by automated culled search rather than manual linear search: c.f. git log -p and git log -p -S 'regex_for_change'. I'm not aware of any vim feature (or Gundo feature) that lets you specifically target a part of the diffs. For example, it is very easy to add power to git log -p by just using the search in less, but this can't be done in Gundo. – Steven Lu Jul 15 '13 at 20:28
  • Ah, correction: -S for git pickaxe search for string, -G for git pickaxe search for regex. Quite powerful. – Steven Lu Jul 16 '13 at 0:01
  • In Feature request: Search through undo history, the author of the Undo-tree plugin suggested a simple algorithm that could be used to implement this functionality in Vim: loop until undo is not available { search "the string"; undo one step; }. Undo status is available through Vim’s undotree() function. – Rory O'Kane Feb 25 '15 at 17:16
  • See vi.stackexchange.com/q/2220/1060 – Kyle Strand Feb 26 '15 at 20:20

It looks like what you want to do is the exact use case for Gundo.

  • 1
    The perils of multitasking: you start to type an answer, then you have to deal with an "emergency", you come back and finish your answer, hit the button and notice that there's already a similar answer. With a beautiful screenshot. – romainl Apr 25 '12 at 14:34
  • 1
    Gundo is extremely useful, but is there an easy way to search the various versions of the file shown in the undo tree? – Kyle Strand Jan 20 '14 at 21:11
  • @KyleStrand, I don't think you can be more granular than Gundo's diff feature. – romainl Jan 20 '14 at 21:19
  • 1
    I'm not really asking for granularity, I'm asking for search capabilities. Specifically, I'd like the ability to submit a search query that would search through all of the diffs shown by Gundo and display something like a list of all the versions of the file that have that search pattern in their diff. Superficially, this seems like what the original question here is asking, but since your answer doesn't really answer mine and has been "accepted" anyway, I was thinking I should just write a new question. – Kyle Strand Jan 20 '14 at 21:24
  • 3
    I don't mean to be rude, but given that we're both familiar with Gundo and neither of us thinks that Gundo does what I'm want, I'm not sure why your response is "read the documentation." I'm not even sure why you assume that I haven't read the documentation. The point is that your answer simply doesn't answer my question. – Kyle Strand Jan 20 '14 at 22:13

Have you tried :history? This shows the command history (edited and emphasized after comment) registers that are currently available. See :help history in Vim for more information.

For the history buffers regarding input, see :h undo-redo (specifically the :h :undolist section).

Also see How is Vim's undo tree used? (SO) and Using undo branches (Vim wiki).

  • That's the command history, not the history of the file. – Dave Vogt Apr 25 '12 at 13:17
  • @DaveVogt: So it is, updated my answer with the correct help section for your request. – Daniel Andersson Apr 25 '12 at 13:21
  • Thanks for the links. The second one has an answer pointing to bitbucket.org/sjl/gundo.vim which is already pretty close. – Dave Vogt Apr 25 '12 at 13:44

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.