22

Let me pose the question this way. I open a new file in vim, (version 1)

#include<stdio.h>
main()
{
...blah
}

and then use <Esc>:w<Enter> to write the file. Then made changes (version 2)

#include<stdio.h>
main()
{
...blah
... edit1
... edit2 //and large number of changes here and there in code
}

then I save changes using <Esc>:w<Enter>.

Is there a way to undo changes to version 1 directly (Since it was a last save) i.e., without constantly pressing u for undoing

7
  • 4
    Quit wasting your time and get the gundo plugin
    – Andy Ray
    Aug 29, 2013 at 0:41
  • 3
    Gundo or use version control software
    – FDinoff
    Aug 29, 2013 at 0:42
  • @FDinoff Actually, I use svn. But the issue is, I pull the updates from server and make a lot of local edits (without committing, as commit affects fellow users) and finally commit after a long interval!
    – SKPS
    Aug 29, 2013 at 0:56
  • @SathishKrishnan I'm too used to git where commits don't effect other users until you push
    – FDinoff
    Aug 29, 2013 at 1:00
  • 1
    @SathishKrishnan git comes with git svn which lets you use git locally and svn remotely.
    – Dean
    Aug 29, 2013 at 1:44

3 Answers 3

33

From Vim's help:

:earlier {N}f   Go to older text state {N} file writes before.
                When changes were made since the last write
                ":earlier 1f" will revert the text to the state when
                it was written.  Otherwise it will go to the write
                before that.
                When at the state of the first file write, or when
                the file was not written, ":earlier 1f" will go to
                before the first change.

So, if you didn't make changes after the second save, you can do what you want with:

:earlier 1f

On the other hand, if you did unsaved changes after the second save, then:

:earlier 2f

will solve your problem.

See :help :earlier, :help :undolist.

1
  • If you plan on using this a lot make sure you turn on persistent undo. (:h persistent-undo)
    – FDinoff
    Aug 29, 2013 at 0:59
5

You can get all the way back to when you first opened the file pretty easily. Just type a number before u.

10000u, will undo 10000 times. If that's not enough try 1000000u :)

If you want to undo bit by bit, you can do it in any increment, try 5u.

If you just want to reload the file from disk use :e.

3
  • Thanks. After, I do :e is it possible to revert to version 2 by using <Ctrl-r>? I will try it meanwhile..
    – SKPS
    Aug 29, 2013 at 0:24
  • 1
    @SathishKrishnan No, I don't think that's possible. Maybe if you start using multiple buffers to edit one file, but that could get very confusing and you could end up losing y our changes.
    – Paul
    Aug 29, 2013 at 0:31
  • 4
    This only works if you don't use persistent undo. (Which in my opinion is a very nice feature)
    – FDinoff
    Aug 29, 2013 at 0:34
1

Put this into your .vimrc. In normal mode shift u will undo till previous save. Pressing again it will undo till the save before that. You can still go back to original point with a lot of <C-R> if you dint made any changes.
nnoremap U :ea 1f<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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