How to undo in Vim editor in Linux
Last Updated :
25 Sep, 2023
Vim (short for Vi Improvised) is a popular text editor mainly used in Linux and Unix-based operating systems but due to having a CLI (command line interface), it might be difficult for new users to make use of basic features such as undo in Vim. So in this article, we will learn different ways and types to undo changes in Vim Editor in Linux.
Like all other modern text editors, Vim also has the functionality UNDO which enables the users to revert the changes done in the document. It saves the changes as revisions or entries in the memory so that when asked, it can revert the current revision to the previous revision.( basically undoing changes).
Vim has also a criterion of what is to be considered an entry or revision. Any of the following can be considered an entry:
- Pressing i to move into insert mode.
- Writing 4 new lines in insert mode.
- Editing 4 different lines in insert mode.
How to Undo in Vim Editor?
Vim not only offers basic undo function, but also of various types with different affects. We can undo changes in three different ways:
- Undo Most Recent Change
- Undo Multiple Changes
- Undo All Changes in a Single Line
Lets look at each of them one by one.
1. Undo Most Recent Change
In this method, we undo the last change we did on the document. In other words, we revert to the most recent revision of the document.
Command
u or :u or :undo
To do this, follow the given steps:
- Press Esc - To exit the insert mode and come to the normal mode.
- Enter u, :u or :undo - To enter undo the latest change.
Before Undo
- You can see the most recently done changes are reverted.
After Undo
2. Undo Multiple Changes
We can also undo multiple changes in a single command.
Command
Nu or :undo N
where N is the number of changes you want to undo.
Follow the given steps to do undo multiple changes in one go:
- Press Esc - To exit the insert mode and come to the normal mode.
- Enter 3u or :undo 3 - To undo the last 3 changes.
Before Undo
- As you can see, the current revision is reverted to the third most recent revision.
After Undo
3. Undo All Changes in a Single Line
This method allows the user to undo all the changes in a Single Line.
Command
U
Follow the given steps to undo all the changes in a Single Line:
- Press Esc - To exit the insert mode and come to the normal mode.
- Enter U - To undo all the changes in the given line.
Before Undo- As we can see, all the changes in the single line are reverted.
Listing All Undo Branches
After undo, we do some more changes and overwrite the last done changes with current changes. Generally, there is no way to access those removed changes but in Vim, these changes are not completely deleted but instead, their entries get branched into different undo branches. We can view and access these branches using the following command:
:undolist
Example
UndolistConclusion
Undo is one of the basic operations in any text editor, and Vim has a powerful version of it that offers more capabilities as compared to other editors. This article discusses the different undo types in Vim, which can help you improve your productivity.
Similar Reads
How to Undo in Nano Editor Nano is a lightweight and user-friendly text editor commonly used in Unix-based systems. While editing text, mistakes are bound to happen, and having the ability to undo changes is essential. In this guide, we'll explore various methods of undoing changes in the Nano editor, along with practical exa
3 min read
How to Install Vim Editor in Ubuntu? Vim is a highly configurable and powerful text editor used by developers and system administrators on Ubuntu and other Linux distributions. Whether youâre coding, writing scripts, or editing configuration files, Vim offers efficiency with its vast features and keyboard shortcuts. In this guide, we'l
3 min read
How to Install Vim Editor in MacOS? Vim editor is an open-source software developed by "Bram Moolenaar" in 1991. Vim, short for "Vi IMproved," is a highly customizable and powerful text editor that is most commonly used by developers, system administrators, writers, and anyone who works extensively with text files. Vim is created as a
3 min read
How to Indent Code in Vim Editor? Vim is a popular text editor used by many developers for writing code, creating readme files, and writing scripts. It's open-source and offers a lot of ways to customize your experience. This makes it easier and more efficient to work with. Properly formatting and indenting your code is important be
4 min read
Vim Editor in Linux If youâre new to the world of Linux, one of the first things youâll encounter is the command line interface. And if youâre going to be working in the command line, youâll inevitably come across various text editors. Among them, Vim stands out as a powerful, yet notoriously complex, text editor. In t
7 min read
How to Hide Line Numbers in Vim Editor In this article, we will cover how to hide line numbers in the Vim editor. First, we look at what Linux and VIM editors and why we use them, what are its features and how can we use them followed by various options to make the Vim editor hide line numbers and how we can change the default settings o
3 min read