Undo and Redo Text in Vim Editor

While making changes to a text document, it is common to make changes multiple times. It is not worth it to rewrite text again and again, especially if you are writing something important and you feel that the previous words or code were right compared to this, or vice versa.

Therefore, we have a special option in the editor called “Undo and Redo,” which makes our task a bit easier.

So, let us see how you can use the Undo and Redo options in the Vim editor to manipulate text.

Create a sample file to explore undo and redo in Vim

Although we have created a sample file to perform cut, copy, and paste operations in the last post, if you haven’t followed our guide, no problem, we will again create it for you.

First and foremost, you need to create a sample file that will list the operating systems in sequence. To create the sample file, open your VIM editor and paste the following line:

I assume you know how to copy and paste the below lines of text into the VIM editor without writing them manually. Use this shortcut (Ctrl + Shift + V) to paste in VIM.

Basic fact: To get into insert mode, use "i"To get out, press "ESC" for normal mode, and whatever keybinding you will apply for undo and redo, first get out of the insert mode and then try.

1.  Ubuntu 20.04 LTS
2.  Debian 10
3.  Fedora 36
4.  Debian 11
5.  AlmaLinux 8.6
6.  Ubuntu 22.04 LTS
7.  CentOS Stream
8.  RHEL 9
9.  POP!_OS
10. Arch Linux

Once you have created the file, we will apply the undo and redo operations to the respective lines of text.

How to Undo text changes in the VIM editor

When you start writing content on a text file, Vim gets in action mode and records all the changes that have been made by you, and you can leverage this tracking system to comfort your fingers, which are doing a lot of work for you.

Anyway, you can use u, :u, :undo keybindings or commands to make changes undo in Vim.

Undo last changes in Vi/Vim

To undo the last changes, you can use any one of these u :u or:undo among them.

For example, while appending the above list, I thought I should add more operating systems to the list, so I thought, why not add Microsoft operating systems to the list? I added “Windows 11” to the list, but Windows 11 is not looking great among the FOSS OS.

So to undo the last changes, remove “Windows 11” from the list.

  1. First, come back to normal mode by pressing the "ESC" key
  2. Now you can press u, :u, :undo to undo the changes.
u

The changes will look like the below images.

Undo multiple lines of changes in Vi/Vim

To undo multiple lines of change, you can use a quantifier to express the number of changes you intend to make undo.

uN

For example, I removed five distribution names from the sample file by pressing dd that we created on top, but later I remembered that I needed them for the next section, so to undo, I can use “u” for a single change, but I need something that can do multiple undos at once.

So to make that I need to follow the below steps

  • First, come back to normal mode by pressing the "ESC" key
  • Now you can pass a number of changes you want to undo for example I want to undo 0 lines of changes.
0u

The output of the below command

Undo multiple lines of changes in Vi/Vim

You can find a number of undoing changes using:undolist

Undolist record
Undo list record
  • number: It’s increasing when you make a number of undos.
  • changes: It reflects the number of changes per undo you have made.
  • when: Show timing when you have done undo.
  • saved: changes written to the disk. It can be useful to go back to the previous save point with the help of :later and :earlier commands.

Instead of using u0, I can also make the same change through redo options.

How to Redo text changes in the VIM editor

If after making undo, you thought the previous word or character was right and you wanted to get it back, then you need to press a combination of two keys, "Ctrl + r", or else you can also use the ":redo command to redo your changes.

In the above section, I have undone “is one of the best distros”, which was next to Arch Linux, but now I want to redo those changes, and after the changes, the last line should look like “Arch Linux is one of the best distros”.

To redo, follow the below steps:

  • Get into normal mode (ESC)
  • You can press “Ctrl + r” or use the “: redo” command.
Ctrl + r
   OR
:redo

Similarly, for multiple redos, you can use a quantifier to express a number of redo changes like “4Ctrl + r”.

TL;DR

Whenever you want to undo your last changes, use “u”, or “:undo”, and to redo your last undo changes, use “Ctrl + r”.

  • Undo command in Vim
    • u, or, u, or :undo
  • Redo command in Vim
    • Ctrl + r or :redo

That’s all to undo and redo your last changes in Vim.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments