How to undo and redo changes in vi/vim

vi/vim editor FAQ: How do I undo and redo changes in the vi and vim editor?

Solution: Undo changes in vim with the u command in command, and redo changes using the [Ctrl][r] keystroke. See below for more details.

vim undo (how to undo a change in vi/vim)

You “undo” changes in vi and vim with the undo command, which is the u key when you are in vim command mode. For instance, if you start with this text in your editor:

aaa
bbb
ccc

Then delete the second line with the vi dd command, you can then undo this change by typing the letter u while still in command mode. This undo command works with other changes as well, including inserts, deletes, search and replace functions, etc.

vim redo (how to redo something you just un-did)

Update: As noted in the Comments section below, the best way to redo a vim command is to use the [Ctrl][r] keystroke.

Another approach to redo a command in vi/vim is to enter the “redo” command in last-line mode, like this:

:redo

For instance, if you wanted to again delete that second line, you could type this command to re-do that change. But again, since learning about it, the [Ctrl][r] keystroke is a much faster way to undo a change in vim.