19

In Vim, is there a way to select a block of text and use undo to only undo changes to that block of text?

Let's say I rewrite a function, then go and make some changes elsewhere in my file. Afterwards, I realize that my first function implementation was indeed better. I'd like to undo the changes I made in that function, but leave my subsequent additions intact.

I don't know if this is even possible, but I often find myself wanting this feature.

justin
  • 1,147
  • 1
  • 8
  • 17
  • I'm sorry I can't help but nice question. – lucapette Jan 10 '11 at 00:01
  • I'd love to know if there is a good way to do this. +1 – gahooa Jan 10 '11 at 00:07
  • 1
    Possibly same question here: http://stackoverflow.com/questions/2236099/how-can-i-undo-all-changes-to-a-block-of-text-in-vim – Samet Atdag Jan 10 '11 at 00:13
  • 3
    I think that's what Git/Mercurial for. – tungd Jan 10 '11 at 02:25
  • 2
    Man, I can't even start to consider how Vim would handle this in the background. The undo stack is just that - a stack. If you start messing around with ripping pieces of it out, then it's not longer a stack and you end up with what amounts to a tree with multiple undo paths - very very very ugly. – Marc B Jan 10 '11 at 03:55
  • I agree with tungd, that's what SCM softwares are for... If you want vim integration of git, I recommend vim-fugitive, it's such an awesome add-on. It will let you easily diff current version with commited version, and replacing a block is done in a few keystrokes. – François Jan 10 '11 at 09:21

2 Answers2

3

Currently... No. Vim 7.3 has undo branches that you can traverse but as far as I know Vim does not pay attention to any selected text during an undo.