List of content you will read in this article:
If you do any kind of text editing on the Linux command line including editing configuration files, you will probably meet up with Vi or Vim.
Considering, Vim is the modern equivalent of VI. In this article, we are going to talk about how to undo and redo in Vim/Vi.
We also are going to share with you lots of useful information in this regard. To learn everything about Vi/Vim undo and redo process keep along with us!
What is Vim/Vi editor?
Vim/Vi is a very interesting and kind of strange text editor. However, you really do need to know how to use it because it really can help you in those moments when you want to do text editing from the command line.
How to redo Multiple Changes in Vim/Vi editor?
To repeat the last changes in Vim/Vi editor, you can easily follow the below steps:
- Go to your terminal and open the required file.
- Press the “ESC” button to return to command mode.
- Create the desired changes.
- Redo the changes as many times as you want using the following syntax:
[number]Ctrl+r
Note 1:
To redo the Vim command for executing multiple changes, you need to write the number of times you want that exact change repeated. Then you should press and hold the “Ctrl” button while typing the “r” letter.
Note 2:
Vi undo and redo process is the same as Vim.
How to redo the last command in Vim?
Vim redo last command is very easy. You might simply press and hold CTRL + r simultaneously
How to undo in Vim/Vi editor?
If you are wondering how to undo changes in Vim/Vi editor, or if you want to know about Vim undo an undo command, you can simply follow the below instruction:
- Go to the Linux terminal and open your file.
- Create some changes.
- Type in the following syntax:
u
Or:u
Or :undo
Or
:undo N
:undo 3U
- Press the “Esc” button to go back to the normal mode.
Note 1:
The late syntax helps you to undo an undo in Vim and all of the changes you have created lately.
Note 2:
Undo and redo in Vi editor is the same as in Vim.
Note 3:
To Undo the two latest changes, type in the following syntax:
2u
List Undo Branches
One thing you might not know about Vim is that behind the scenes, to do its undo and redo processes, it actually maintains a tree structure. The best way to think of a tree is to think of a Git repo and how it has a bunch of different branches. Basically, it is the same sort of idea.
Therefore, when you make a modification in Vim, it is going to branch in some other direction. Now, when you use undo and redo in Vim, it is not entirely clear that you are working with a tree. Because Vim is going to decide the best branch to jump onto, however, you can move around this tree normally.
The problem is though, if you are working with a tree and you do not have a visual representation of it, it is going to be very hard to work out where you are going to be jumping to. So, you can use plugins such as UndoTree to add a visual representation of the tree and list undo branches to make navigating it much easier.
Example about using undo branches in Vim
Here are some examples to make you more familiar with using undo branches in Vim:
- Go to your terminal application.
- Make a demo file using the following syntax:
$ vim demo.txt
- Press “i” to add the below text:
Linux macOS Unix monovm
- Remove the word MacOS by pressing “x” as many letters as the word contains (five times):
Linux Unix monovm
- To undo the latest changes, you need to press “u” five times:
Linux macOS Unix monovm
How to undo and redo in Vim 3?
Undo and redo in Vim 3 is exactly the same as in other versions. Therefore, you can follow the above instruction to edit your file and have a smooth experience of undoing and redoing Vim files.
List tree of changes
While you may use the Vim undo and redo commands, the changes you make in this editor have never remove completely. Therefore, you can access and list tree of changes using the following command:
:undolis
Note:
The output would be something like the above picture. Each column has its specific description:
- The first column shows the number of changes, which will be increased whenever you create any removable changes.
- The second column identifies the number of changes created from the root.
- The third column shows the exact date and time for creating changes.
- The fourth column identifies the files related to changes you recently made. To observe these files, you can use the “:later” or “:earlier” commands using the following syntaxes:
:earlier {count} (Go to the earlier text state {count} times)
:earlier {N}s (Go to earlier text state {N} seconds before)
:earlier {N}m (Go to the earlier text state {N} minutes before)
:earlier {N}h (Go to the earlier text state {N} hours before)
:earlier {N}d (Go to older text state {N} days before)
:later {count} (Go to the newer text state {count} times)
:later {N}s (Go to the newer text state {N} seconds later)
:later {N}m (Go to the newer text state {N} minutes later)
:later {N}h (Go to the newer text state {N} hours later)
:later {N}d (Go to the newer text state {N} days later)
For example, you can use the following syntax to go back four days:
:earlier 4d
📈 Scale your business without limits! Monovm's Linux VPS hosting enables seamless growth and empowers your website to handle increased traffic effortlessly. 💼🚀💻
Plugins
Some of the most useful Vim plugins are as follows:
1. NERDTree
NERDTree is one of the most popular Vim plugins ever made. You can think of it as a replacement for the file explorer. It makes it a little more similar to what you might be used to in TextMate. Using this tool, you can browse complex folder structures and open any file for editing, or undo and redo changes in Vim.
2. Emmet-vim
This is an awesome code completion plugin, which allows you to undo-redo in Vim, and write HTML and CSS super efficiently. Using this tool, web developers can get the structure quickly.
3. Vim-gitgutter
this is one of the most useful Vim plugins for Git integration. This awesome tool lets you easily find out if the lines have been changed or edited. Also, you can jump between the changes and move back and forth on them. In addition, it helps you to fold the unchanged text.
4. Commentary.vim
Commentary.vim is a plugin that makes it easier to do commenting across all the different sorts of files. So, you can manually write out the comments in Vim if you want to; but it would be easier if you just have a consistent binding across all your files. So say you are in a Vim file, and you want to add a comment you can press this binding, and it will add a quotation mark.
5. Fugitive
Fugitive is another useful plugin, which is mostly used in Git integration. This tool is a Git command wrapper with a syntax similar to Git. In addition, if you want to customize your commands to view a tree, commit, move a file, and do many more activities on high-level operations, you can use this plugin.
6. Asynchronous Lint Engine (ALE)
This tool is mostly useful for checking the codes in real-time, finding semantic errors, and making changes as required. Using this plugin, you can find quick suggestions that help you to fix syntax errors.
The Bottom Line
Here, we talked about Vim undo and redo process and everything you should know. If you have any feedback or questions, please let us know. Good luck.
People Are Also Reading:
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'.