Search in Vim's undo history

6

1

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.

Dave Vogt

Posted 2012-04-25T12:16:46.487

Reputation: 1 470

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 – 2015-02-25T17:16:08.280

See http://vi.stackexchange.com/q/2220/1060

– Kyle Strand – 2015-02-26T20:20:46.577

1I 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 – 2013-07-15T20:28:11.160

Ah, correction: -S for git pickaxe search for string, -G for git pickaxe search for regex. Quite powerful. – Steven Lu – 2013-07-16T00:01:44.517

Answers

-1

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

romainl

Posted 2012-04-25T12:16:46.487

Reputation: 19 227

1The 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 – 2012-04-25T14:34:41.180

1Gundo is extremely useful, but is there an easy way to search the various versions of the file shown in the undo tree? – Kyle Strand – 2014-01-20T21:11:41.757

@KyleStrand, I don't think you can be more granular than Gundo's diff feature. – romainl – 2014-01-20T21:19:53.433

1I'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 – 2014-01-20T21:24:42.270

No, what you should do is follow the link in my answer and look for what you want in the documentation. AFAIK it doesn't do what you want, though. – romainl – 2014-01-20T21:32:24.983

3I 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 – 2014-01-20T22:13:09.567

0

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

Daniel Andersson

Posted 2012-04-25T12:16:46.487

Reputation: 20 465

That's the command history, not the history of the file. – Dave Vogt – 2012-04-25T13:17:29.077

@DaveVogt: So it is, updated my answer with the correct help section for your request. – Daniel Andersson – 2012-04-25T13:21:16.220

Thanks for the links. The second one has an answer pointing to
http://bitbucket.org/sjl/gundo.vim/ which is already pretty close.

– Dave Vogt – 2012-04-25T13:44:32.740