vim undo

35 views
Skip to first unread message

Chris Jones

unread,
Mar 4, 2020, 1:01:06 PM3/4/20
to vim...@googlegroups.com
Scenario:

- I open a file
- I make one big global change (C0)... say via substitute+regex
- I proceed to make more changes (C1, C2, ... Cn).

While *still editing the file* I realize that the initial change (C0)
was in error.

Can I undo the initial change (C0) and keep all the ensuing changes?

Or...

Go back to the initial state of the buffer, and make vim reapply all the
C1-Cn changes?

Standard vim only please, no plugins.

Thanks,

CJ

Tim Chase

unread,
Mar 4, 2020, 1:20:01 PM3/4/20
to Chris Jones, vim...@googlegroups.com
I don't think there's a native way to play back all the changes as a
rebase. If I had a dire need to do as much, I'd do a little
do/undo/diff/patch tango, something like

:w current.txt

then undo to C1

:w c1.txt

then undo once more take you back to the pre-C0 change and save that:

:w pre-c0.txt

Then, in the shell

$ diff -u c1.txt current.txt > rest_of_the_changes.txt

and run that patch against the pre-c0.txt

You might be able to use git to checkin various points and then
use its rebasing to help you out.

But in pure vim? Not much.

-tim


Bram Moolenaar

unread,
Mar 4, 2020, 5:22:22 PM3/4/20
to vim...@googlegroups.com, Tim Chase, Chris Jones
You can do it without leaving Vim:

:w new_version
(undo until the big change is undone)
:vimdiff new_version
(diffget to get the changes you want)

If there are overlaps it's more work. If there are many changes then
the diff + patch tools might work better, but likely "patch" will find
conflicts and it's not good at dealing with those.

--
hundred-and-one symptoms of being an internet addict:
175. You send yourself e-mail before you go to bed to remind you
what to do when you wake up.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Chris Jones

unread,
Mar 5, 2020, 11:03:44 AM3/5/20
to vim...@googlegroups.com
On Wed, Mar 04, 2020 at 01:00:54PM EST, Chris Jones wrote:

Thanks to both for timely response.

Interesting answers indeed.

Looking at my problem from a different angle I eventually noticed
something that allowed me to single out the outcome of the incorrect
changes and let me reverse them without affecting the ensuing editing.

Thank you.

CJ
Reply all
Reply to author
Forward
0 new messages