How do I do redo (i.e. "undo undo") in Vim?
Asked Answered
B

10

728

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

Bright answered 12/10, 2009 at 17:10 Comment(7)
as I already collected 2 close votes, I think this belongs here as vim is a programmer's editor.Bright
I agree, this is programming-related and it's a real question.Sophistic
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.Wainscoting
@Brian: Do you have a link to the document where this consensus was reached?Preclude
meta.stackexchange.com/questions/22963/…Vankirk
meta.stackexchange.com/questions/20251/… 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.Wainscoting
@Elyasin: Existing questions are not migrated to new networks.Divest
E
942

Ctrl+r

Ephesians answered 12/10, 2009 at 17:12 Comment(6)
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.Ilium
If I am reading about vi, then my fingers just act on their own. The vi commands do not work on stackoverflow. :-)Harrell
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 !Resound
@Resound I just used Ctrl+r in vim under macOS's Terminal app - no problems here.Rosaleerosaleen
Maybe @Resound meant Cmd + r messes things up? It certainly does something weird; but Ctrl + r just worked fine for me (Mac + tmux + vim)Vladimar
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.Attar
A
139

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

Ayannaaycock answered 12/10, 2009 at 18:20 Comment(3)
@amindfv: take the number from :undolist and type :undo 178 (say) to rewind to step 178.Ayannaaycock
is there any way to show what exactly will be undo'ed in :undolist?Aborn
@Aborn Use the undotree plugin (github.com/mbbill/undotree)Diestock
E
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.

Ensue answered 23/6, 2012 at 11:38 Comment(4)
:later worked for me in vi (not vim -- searched for "vi" on google but all results are for "vim"), thanks!Cecilia
@Cecilia Almost everywhere where you have vi command it is a symlink to a vim executable or a copy of it. Not completely everywhere though.Ensue
@Ensue 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.Cecilia
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 ...Amendatory
V
48

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}
Vankirk answered 12/10, 2009 at 17:15 Comment(0)
N
32

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.

Nominalism answered 12/10, 2009 at 17:12 Comment(3)
How about in insert mode?Schlieren
@YuFengShen the power of vim is in the command mode. we don't need redo in insert modeSatisfaction
The link is (effectively) broken.Tu
B
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

Brusquerie answered 28/11, 2015 at 11:37 Comment(0)
G
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.

Grouse answered 17/2, 2014 at 3:22 Comment(0)
D
5

CTRL+r

The "r" is lower-case.

Deeann answered 12/10, 2009 at 17:14 Comment(3)
Actually, it shouldn't matter -- traditional consoles have no distinction between ^r and ^R, and Vim follows that.Disconcerted
@ephemient: agreed. In MS-DOS, you can't even type a lowercase ^X (where X can be any letter).Sultana
In Vim, Ctrl-shift-R (ctrl with uppercase R) is replace mode.Wicket
G
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.

Gaselier answered 8/2, 2018 at 8:35 Comment(3)
Downvoted because the question is asking specifically about vim, not Visual Studio.Gum
@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?Gaselier
Unfortunately this no longer seems to work? Quite annoying, as undo/redo's are something VsVim seems to be especially bad atLorraine
D
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

Dantedanton answered 12/10, 2009 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.