725

In Vim, I did too much undo. How do I undo this (that is, redo)?

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • 110
    as I already collected 2 close votes, I think this belongs here as vim is a programmer's editor. – flybywire Oct 12 '09 at 17:17
  • 30
    I agree, this is programming-related *and* it's a real question. – DigitalRoss Oct 12 '09 at 17:19
  • 44
    Someone around here goes around and marks every Vim question as "belongs on SU" despite the community having long ago decided that Vim belongs on SO, it being a programming tool. – Brian Carper Oct 12 '09 at 19:45
  • @Brian: Do you have a link to the document where this consensus was reached? – too much php Oct 13 '09 at 02:08
  • http://meta.stackexchange.com/questions/22963/where-should-developer-computer-setup-questions-go – geowa4 Oct 13 '09 at 12:47
  • 12
    http://meta.stackexchange.com/questions/20251/where-to-ask-a-question-about-an-ide but I recall this coming up last year sometime. Vim/Emacs are IDEs. Moreover the languages used in the config files for both are Turing-complete scripting languages, so the act of setting up Vim is itself programming to begin with. – Brian Carper Oct 14 '09 at 17:38
  • @Elyasin: Existing questions are not migrated to new networks. – SabreWolfy Jul 18 '16 at 14:19

10 Answers10

937

Ctrl+r

John Millikin
  • 197,344
  • 39
  • 212
  • 226
  • 2
    I would just like to throw the undoTree plugin out there, super awesome for the times you have seemingly screwed yourself with a flurry of undos and redos. – Jake Sellers Jan 03 '14 at 05:31
  • 3
    If I am reading about vi, then my fingers just act on their own. The vi commands do not work on stackoverflow. :-) – ciscogambo Jan 17 '14 at 21:17
  • 3
    Note to Mac users: Ctrl + r in a Mac terminal will ruin your day in vim. Don't know the proper key combo in Mac terminal to undo, but it's most definitely NOT Ctrl + r ! – tpartee Nov 03 '17 at 23:58
  • @tpartee I just used Ctrl+r in vim under macOS's Terminal app - no problems here. – Jase Jan 12 '18 at 04:13
  • 3
    Maybe @tpartee meant `Cmd + r` messes things up? It certainly does something weird; but `Ctrl + r` just worked fine for me (Mac + tmux + vim) – dwanderson Jun 07 '18 at 18:23
  • For some reason, in my case `Ctrl + r` sends Vim into the background process instead of un-doing. So I need to do `fg` in order to bring it back. Not sure what is the reason. Using `iTerm2` to connect an Ubuntu machine with `zsh` (and oh-my-zsh) installed. – devforfu Feb 21 '20 at 06:44
137

Also check out :undolist, which offers multiple paths through the undo history. This is useful if you accidentally type something after undoing too much.

Peter
  • 127,331
  • 53
  • 180
  • 211
55

Use :earlier/:later. To redo everything you just need to do

later 9999999d

(assuming that you first edited the file at most 9999999 days ago), or, if you remember the difference between current undo state and needed one, use Nh, Nm or Ns for hours, minutes and seconds respectively. + :later N<CR> <=> Ng+ and :later Nf for file writes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • 1
    `:later` worked for me in `vi` (not vim -- searched for "vi" on google but all results are for "vim"), thanks! – Luc Jun 09 '14 at 18:49
  • @Luc Almost everywhere where you have `vi` command it is a symlink to a vim executable or a copy of it. Not completely everywhere though. – ZyX Jun 13 '14 at 06:23
  • @ZyX I wish that was true. I only use vi after bash throws a `vim: command not found`. Next thing I do is `alias vim=vi` because I type it so automatically, but it's the plain old vi nevertheless. Also, redo (ctrl+r) would have worked properly had vi been an alias for vim. – Luc Jun 13 '14 at 12:05
  • 2
    9999999d is 27395 years ago. I assume the undo feature was not implemented in vi back then. You might want to save yourself some exhaustive typing effort and use a reasonable number. 9999d correspongs to about 27 years, 999d to 2.7 years. I guess in most cases that's enough ... – ChristophK Jul 06 '18 at 09:08
47

Vim documentation

<Undo>      or                  *undo* *<Undo>* *u*
u           Undo [count] changes.  {Vi: only one level}

                            *:u* *:un* *:undo*
:u[ndo]         Undo one change.  {Vi: only one level}

                            *CTRL-R*
CTRL-R          Redo [count] changes which were undone.  {Vi: redraw screen}

                            *:red* *:redo* *redo*
:red[o]         Redo one change which was undone.  {Vi: no redo}

                            *U*
U           Undo all latest changes on one line.  {Vi: while not
            moved off of it}
geowa4
  • 40,390
  • 17
  • 88
  • 107
31

In command mode, use the U key to undo and Ctrl + r to redo. Have a look at http://www.vim.org/htmldoc/undo.html.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Xinus
  • 29,617
  • 32
  • 119
  • 165
15

First press the Esc key to exit from edit mode.

Then,

For undo, use u key as many times you want to undo.

For redo, use Ctrl +r key

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amol Udage
  • 2,917
  • 19
  • 27
11

Refer to the "undo" and "redo" part of Vim document.

:red[o] (Redo one change which was undone) and {count} Ctrl+r (Redo {count} changes which were undone) are both ok.

Also, the :earlier {count} (go to older text state {count} times) could always be a substitute for undo and redo.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wangxinalex
  • 111
  • 1
  • 4
5

CTRL+r

The "r" is lower-case.

Mwiza
  • 7,780
  • 3
  • 46
  • 42
mimetnet
  • 657
  • 5
  • 11
4

Using VsVim for Visual Studio?

I came across this when experimenting with VsVim, which provides bindings for Vim commands in Visual Studio.

I know about Ctrlr in Vim itself, but this particular binding does not work in VsVim (at least not in my setup?).

What does work however, is the command :red. This is a little bit more of a hassle than the above, but it is still fine when you really need it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • Downvoted because the question is asking specifically about vim, not Visual Studio. – John Von Neumann Nov 07 '18 at 23:24
  • 5
    @JohnVonNeumann This *is* related to Vim, or at least vim bindings, just in the context of VS. That`s what brought me to this question, after all. Punishing someone for adding a little more info seems a little harsh and uncalled for don't you think, even if it does perhaps expand slightly beyond the scope of the OP? – Kjartan Nov 08 '18 at 07:47
  • Unfortunately this no longer seems to work? Quite annoying, as undo/redo's are something VsVim seems to be especially bad at – Josh Wright Sep 01 '21 at 16:46
3

Practically speaking, the :undolist is hard to use and Vim’s :earlier and :later time tracking of changes is only usable for course-grain fixes.

Given that, I resort to a plug-in that combines these features to provide a visual tree of browsable undos, called “Gundo.”

Obviously, this is something to use only when you need a fine-grained fix, or you are uncertain of the exact state of the document you wish to return to. See: Gundo. Graph your Vim undo tree in style

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gregory
  • 10,969
  • 2
  • 30
  • 42