+1 (506) 500-5957 sales@monovm.com

How to Undo and Redo in Vim/Vi editor? [VIM Redo & VIM Undo]

Did you delete something mistakenly while working with the VIM editor? this article will let you know how to undo and redo in VIM/VI Editor. You will also have a brief understanding of VIM Redo and VIM Undo Commands.

Posted: 18 Jan, 22 Updated: 01 Jun, 23 by Susith Nonis 3 Min

List of content you will read in this article:

When editing a file, you may accidentally remove a line or change your mind about an alteration, and you'll want to undo your changes. We will guide you with vim undo and VIM redo commands to accomplish the result about How to undo and redo in VIM/VI editor by following the steps outlined in this article. Vi or Vim is installed by default on macOS and virtually all Linux versions. When your preferred editor is unavailable, knowing the basics of Vim might come in handy.

Vim undoes changes by entries. An entry can be anything you perform inside a single session in insert mode. Any modifications made between pressing the key I (to go into insert mode) and the key Esc (to return to normal mode) are treated as single entries. An entry can also be a Linux command that you use after pressing Esc. For example, you can remove lines and copy and paste text with Vim. A new entry is usually produced to reflect the change when moving between modes.

  1. Hit strong>Esc/strong> to return to normal mode.
  2. Then, using one of the instructions listed below, undo the modifications performed in the preceding entry: strong>u/strong>, strong>:u/strong>, or strong>:undo/strong>.
  3. The preceding entry's modifications and additions will be deleted.
  4. Each line in the file below, for example, represents a single entity. The last line in the text is deleted using the: undo/strong> command.
  1. Before running any Vim commands, use strong>Esc/strong> to verify you're in normal (command) mode.
  2. If you want to reverse multiple modifications in a document at once, you must provide the number of changes in the document. This is accomplished by appending the number before the u/strong> command using the syntax: u/strong>strong>[number]u/strong>.

To demonstrate, we executed the following command in our Vim file: 5u

5u undoes the last five lines of the text since each line was inserted as a distinct entity.

Linux  VPS
Linux VPS

Starting From $5.99/Monthly

  1. Using the critical combination strong>Esc/strong>, enter command mode.
  2. Use the command strong>U/strong> to undo the most recent modifications inside a single line. Please make certain that it is capitalized.

In our example, the instruction restores the changes performed in the previous line, where the last sentence was put in a later line.

  1. In Vim and Vi, use Ctrl-R or type :redo: to undo a modification.
  2. To return to normal mode, use the Esc key.
  3. To undo the last modification, press and hold Ctrl+R. Quantifiers are also available in Vim. If you wish to undo the past five changes, for example, press 5Ctrl-R.
  4. Use the redo command to undo each undo command.

After reading this tutorial, we learned to use the undo and redo command in Vim/Vi. Remember to use the command u to undo a Vim / Vi and Ctrl-R change to redo a previously undone modification. I hope this article helps you with the instruction about how to recover lost lines in vim editor If you think is there any others ways that we can do this process let us know via comment box.

People Are Also Reading:

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.