How to go back (ctrl+z) in vi/vim

VimViBackUndoUndo Redo

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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

Solution 3 - Vim

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.

Solution 4 - Vim

Just in normal mode press:

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

Undo and Redo

Solution 5 - Vim

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.

Solution 6 - Vim

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...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSlazerView Question on Stackoverflow
Solution 1 - VimQiauView Answer on Stackoverflow
Solution 2 - VimromainlView Answer on Stackoverflow
Solution 3 - VimmadD7View Answer on Stackoverflow
Solution 4 - VimsimhumilecoView Answer on Stackoverflow
Solution 5 - VimSquirrlView Answer on Stackoverflow
Solution 6 - VimCloud DuanView Answer on Stackoverflow