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

311,772

Solution 1

Ctrl+r

Solution 2

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

Solution 3

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.

Solution 4

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}

Solution 5

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.

Share:
311,772
flybywire
Author by

flybywire

Updated on January 16, 2020

Comments

  • flybywire
    flybywire over 4 years

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

  • ephemient
    ephemient over 14 years
    Actually, it shouldn't matter -- traditional consoles have no distinction between ^r and ^R, and Vim follows that.
  • alexia
    alexia over 13 years
    @ephemient: agreed. In MS-DOS, you can't even type a lowercase ^X (where X can be any letter).
  • Peter
    Peter over 11 years
    @amindfv: take the number from :undolist and type :undo 178 (say) to rewind to step 178.
  • Jake Sellers
    Jake Sellers over 10 years
    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.
  • ciscogambo
    ciscogambo over 10 years
    If I am reading about vi, then my fingers just act on their own. The vi commands do not work on stackoverflow. :-)
  • Luc
    Luc almost 10 years
    :later worked for me in vi (not vim -- searched for "vi" on google but all results are for "vim"), thanks!
  • ZyX
    ZyX almost 10 years
    @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.
  • Luc
    Luc almost 10 years
    @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.
  • csharpfolk
    csharpfolk about 9 years
    is there any way to show what exactly will be undo'ed in :undolist?
  • Admin
    Admin over 6 years
    @csharpfolk Use the undotree plugin (github.com/mbbill/undotree)
  • tpartee
    tpartee over 6 years
    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 !
  • Jase
    Jase over 6 years
    @tpartee I just used Ctrl+r in vim under macOS's Terminal app - no problems here.
  • dwanderson
    dwanderson almost 6 years
    Maybe @tpartee meant Cmd + r messes things up? It certainly does something weird; but Ctrl + r just worked fine for me (Mac + tmux + vim)
  • ChristophK
    ChristophK almost 6 years
    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 ...
  • John Von Neumann
    John Von Neumann over 5 years
    Downvoted because the question is asking specifically about vim, not Visual Studio.
  • Kjartan
    Kjartan over 5 years
    @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?
  • YuFeng Shen
    YuFeng Shen about 5 years
    How about in insert mode?
  • Tomas Hrubovcak
    Tomas Hrubovcak about 5 years
    @YuFengShen the power of vim is in the command mode. we don't need redo in insert mode
  • Paul Stelian
    Paul Stelian over 4 years
    In Vim, Ctrl-shift-R (ctrl with uppercase R) is replace mode.
  • Peter Mortensen
    Peter Mortensen over 4 years
    The link is (effectively) broken.
  • devforfu
    devforfu about 4 years
    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.
  • Joshua Wright
    Joshua Wright over 2 years
    Unfortunately this no longer seems to work? Quite annoying, as undo/redo's are something VsVim seems to be especially bad at