177

In normal text editors [with all due respect to Vim] there is a shortcut Ctrl+Z when you have done something nasty and want to return to the previous version of the text. Like BACK button in Word. I wonder how can you achieve this behaviour in Vim.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Slazer
  • 4,750
  • 7
  • 33
  • 60
  • 1
    http://vim.wikia.com/wiki/Undo_and_Redo – Cyclonecode Sep 15 '12 at 09:28
  • 4
    The Vim documentation is also very good - `:help undo` will find you your answer on this, for example. – Chris Morgan Sep 15 '12 at 09:28
  • 2
    before diving into vim completely, I suggest you go through the [(interactive) tutorial](http://www.openvim.com/tutorial.html) – karth Sep 15 '12 at 09:29
  • 1
    Thank you all and sorry for this q. Must read more next time. – Slazer Sep 15 '12 at 09:52
  • 8
    on linux, CTRL-Z in vi/vim/gvim mean escape to the console, or put this in the background. you then do whatever you want on the console and type fg (foreground) to bring you back into vim edit session. – mancocapac Dec 17 '19 at 02:00

6 Answers6

293

You can use the u button to undo the last modification. (And Ctrl+R to redo it).

Read more about it at: http://vim.wikia.com/wiki/Undo_and_Redo

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Qiau
  • 5,976
  • 3
  • 29
  • 40
  • 3
    I map U to redo so I can undo / redo quickly, the entire edit history if necessary, in vimrc: nnoremap U – NeilG Jul 25 '19 at 03:24
15

The answer, u, (and many others) is in $ vimtutor.

romainl
  • 186,200
  • 21
  • 280
  • 313
14

Just in normal mode press:

  • u - undo,
  • Ctrl + r - redo changes which were undone (undo the undos).

Undo and Redo

simhumileco
  • 31,877
  • 16
  • 137
  • 115
  • I know that most of my answer is duplicated with those already present, **but** the value added of my answer is redo as `Ctrl + r` (lower case `r`) based on the documentation. Nobody before put it here. In addition, I tried to make the answer as clearly as possible. – simhumileco Mar 22 '19 at 02:36
8

Here is a trick though. You can map the Ctrl+Z keys. This can be achieved by editing the .vimrc file. Add the following lines in the '.vimrc` file.

nnoremap <c-z> :u<CR>      " Avoid using this**
inoremap <c-z> <c-o>:u<CR>

This may not the a preferred way, but can be used.

** Ctrl+Z is used in Linux to suspend the ongoing program/process.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
madD7
  • 823
  • 10
  • 23
0

On a mac you can also use command Z and that will go undo. I'm not sure why, but sometimes it stops, and if your like me and vimtutor is on the bottom of that long list of things you need to learn, than u can just close the window and reopen it and should work fine.

Squirrl
  • 4,909
  • 9
  • 47
  • 85
0

I had the same problem right now and i solved it. You must not need it anymore so I write for others:

if you use gvim on windows, you just add this in your _vimrc: $VIMRUNTIME/mswin.vim behave mswin

else just use imap...

Paiusco
  • 305
  • 1
  • 14
Cloud Duan
  • 27
  • 2