Not only can you make changes with Vim or Vi, but you also need to learn how to undo and redo them as you navigate the complexities of these ancient Linux text editors. We’ll go over the key commands and tips in this guide to help you go through your editing history with elegance.

Knowing how to undo and redo changes is essential for ensuring accuracy and efficiency in your text manipulation tasks, regardless of experience level with these potent editors. One essential ability that every Vim / Vi user should possess is knowing how to redo. The ability to undo and redo changes is crucial for any text editor.

Ways to Undo Changes in Vim / Vi

Vim reverses edits made by entries. Anything you perform in insert mode during a single session counts as an entry. After selecting I to enter insert mode and Esc to return to regular mode, any modifications performed are regarded as a single entry.

A command you use after pressing Esc can also be considered an entry. This covers Vim instructions for text copying and pasting as well as line deletion. Generally, a new entry is made whenever you go between modes.

Here you can Undo the Last Change

1. Press Esc to first enter regular mode.

2. Next, use one of the following instructions to reverse the modifications performed in the previous entry: u, :u, :undo.

3. The preceding entry’s additions and modifications will all be eliminated.

For instance, every line in the file below denotes a distinct object. The final line of the document will be eliminated by using the :undo command.

output displays undo last change

The last line is deleted when you press Enter, and the result indicates that there is now one line less.

displays output once press enter

Here you Can Undo Multiple Changes

1. Before executing any Vim commands, make sure you are in normal (command) mode by pressing Esc.

2. You must indicate how many changes in the file you wish to undo to perform several undoes at once. The number is added before the u command in the following syntax to do this: [number]u.

To demonstrate, we used the following command in our example Vim file:

4u

The last four lines in the text are undone by 4u because each line was added as a separate item.

command to undo multiple changes

There should be four fewer lines, according to the output.

Here you Can Undo the Latest Changes in the Line

1. Press Esc to enter command mode.

2. Use command U to undo the most recent modifications made within a single line. Verify the capitalization.

In our case, the command removes the addition of the final sentence in a later entry, which was made within the last line.

output displays latest changes

Now Redo Changes in Vim / Vi changes

1. You must be in the usual mode in Vim (hit Esc) to redo.

2. You can now use the Ctrl key and the r key to undo any changes you’ve already made.

The last undone entry will be redone by Vim.

Here you can Redo Multiple Changes

1. Press Esc to get back to command mode.

2. Use the following syntax to undo many times with a single command: [number]Ctrl+r. To perform the desired number of redos, write it down and then press and hold the CTRL key.

With the Commands List Undo Branches

You will probably undo a modification while working on a file in Vim / Vi and then write over it with new changes. This reversed alteration doesn’t entirely vanish. It turns into a branch that you can locate and utilize at a later time.

Use the following command to see the undo branches:

:undolist

The result you get ought to resemble our example:

run the command to display undo branches

There are four columns in it:

  • Digit. The number of changes.
  • Diversity. The quantity of alterations made to the object.
  • When. the moment the modification was done.
  • Redeemed/Rescue. If and where the file has been kept on disk.

Conclusion

In Vim or Vi, undoing and redoing changes is like having a powerful time-traveling text editing tool at your disposal. You now have more sophisticated control over the past editing you’ve done with commands like u for undo and Ctrl + r for redo.

The more you apply these strategies, the more your editing process in these well-known text editors will flow and operate more smoothly. Now that you know how to edit and update text with ease, you may confidently and deftly handle the intricacies of text manipulation.

You ought to know how to utilize Vim / Vi’s undo and redo commands after reading this article. Recall that the commands u and Ctrl-R can be used to undo and redo changes in Vim or Vim, respectively. Make sure to save the Vim file after you’re done before closing it.