Undo in Vim is one keystroke: press u in Normal mode. Redo is Ctrl-r. That's the whole answer for 90% of cases — but Vim also keeps a full undo tree, lets you jump back in time by minutes, and can even remember your edit history after you close the file. 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, let's get into the commands properly.
📋 Vim Undo and Redo Commands: Quick Answer
Make sure you're in Normal mode first (hit Esc if you've been typing). Then:
| Action | Command |
| Undo last change | u |
| Redo last undone change | Ctrl-r |
| Undo multiple changes | 3u |
| Undo using the Ex command | :undo |
| View undo history | :undolist |
| Move to an older state | :earlier 5m |
| Move to a newer state | :later 10s |
📌 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. If you're new to the ecosystem, it's worth understanding the difference — check out our breakdown of Vi vs Vim and how Vim compares to Nano before choosing your daily editor.
↩️ How to Undo Changes in Vim
Undo the Last Edit
Press Esc to leave Insert mode, then press u. Each press walks one change backwards. Vim counts a whole Insert-mode session as a single change, so if you typed a full sentence and pressed Esc, one u wipes the entire sentence — not one letter.
Undo Multiple Edits
Prefix the command with a count. Typing 5u undoes the last five changes in one go. This is faster than mashing u, and honestly it's more predictable, because you know exactly how far back you went.
Undo with the Ex Command
:undo
:undo 3
:uThe bare form behaves like u. With a number, it's different — and people get this wrong constantly. :undo 3 doesn't undo three changes; it jumps the buffer to change number 3 in the undo history. You'll see those numbers when you run :undolist.
What U Does in Vim
Capital U is the line-undo command. It restores the last line you changed, reverting all edits made to that line while your cursor stayed on it. Press it again and it undoes itself — it toggles. It's a change in its own right, which means u can undo a U. Treat it as a convenience, not a real history tool. In plain vi, U may be the only undo you get beyond a single step.
🔁 How to Redo Changes in Vim
Use Ctrl-r in Normal Mode
Redo undoes your undo. Press Ctrl-r in Normal mode and the change you just reverted comes back. A quick test you can run right now:
i Hello server → Esc
u → text disappears
Ctrl-r → text returnsRedo Multiple Undone Changes
Counts work here too. 4Ctrl-r replays four undone changes. So does :redo as an Ex command, though I rarely see anyone type it.
Redo vs Repeat Last Change
These two get mixed up all the time. Ctrl-r re-applies something you undid. The dot command, ., repeats your last change at the cursor's current position — it creates a brand new change. Delete a line with dd, move down, press . and you've deleted a second line. That's repetition, not recovery.
🌳 How Vim Undo History Works
What the Undo Tree Is
Vim doesn't keep a flat list of changes. It keeps a tree of buffer states. Every change is a node, and undo/redo moves you between nodes rather than "deleting" work. The best way to think of it is like a Git repo — it has a bunch of different branches, and when you make a modification, it branches in some other direction.
Why New Edits Create Branches
- You make change A.
- You make change B.
- You press u — you're back at A.
- You type something new: change C.
C now hangs off A as a new branch. B still exists in the tree, but it's on the branch you walked away from.
Why Redo May Stop Working After a New Change
Because Ctrl-r only walks forward along the current branch. Once you created C, redo has nothing ahead of it, so it says nothing to redo. B isn't lost — you reach it with :undolist and :earlier, not with redo.
📜 How to View and Navigate the Undo Tree
List Changes with :undolist
:undolistYou get a table of change numbers, how many changes each state holds, when it happened, and whether that state was saved. Note the number you want, then jump to it with :undo 7.
Move to Earlier States
:earlier 3
:earlier 2m
:earlier 1h
:earlier 1fPlain numbers count changes. Add m, s, or h for time-based travel — :earlier 2m means "give me the buffer as it was two minutes ago." The f suffix moves back one file-write, which is my favourite when I've saved a broken config and want the last known-good version.
Move to Later States
:later 30s
:later 5
:later 1fOptional Plugin: Undotree
If branches make your head hurt, install the Undotree plugin (or vim-mundo). It draws the tree in a side panel so you can click through states visually. Useful, but not required — the built-in commands cover everything.
💾 How to Enable Persistent Undo in Vim
By default, closing a file throws away its undo history. Turn on undofile and Vim writes that history to disk, so you can reopen the file tomorrow and still press u.
Add this to your ~/.vimrc:
set undofile
set undodir=~/.vim/undo
set undolevels=1000Create the directory once with mkdir -p ~/.vim/undo, otherwise Vim complains. If you edit config files over SSH on a production box, this is the single best setting in this article — undo history survives dropped connections and reboots. Running Vim on a Linux VPS makes persistent undo even more valuable when you're managing remote infrastructure.
🧪 Practical Examples
Undo a deleted line. You pressed dd on the wrong line in /etc/ssh/sshd_config. Press u. The line is back, cursor and all. Yes, this works for deleted words, lines, and whole visual-mode blocks. Want to master deletion first? Here's how to delete lines in Vim/Vi.
Redo after an over-eager undo. You held u too long and lost good work. Tap Ctrl-r the same number of times, or use a count like 6Ctrl-r.
Recover an earlier version. Ten minutes of edits made things worse? Run :earlier 10m, check the buffer, and if you liked the newer version better, come back with :later 10m. Nothing is destroyed either way. New to the editor entirely? Start with how to save and exit in Vim.
⚠️ Common Problems and Fixes
- Ctrl-r does nothing. Usually you're not in Normal mode, or you've hit the end of the current branch after making a new edit. Check with
:undolist. - You're in Insert mode. There,
Ctrl-rinserts register contents instead of redoing. PressEscfirst. - Your terminal or multiplexer steals the key. Some shells map
Ctrl-rto reverse history search, and tmux users often rebind it. Use:redoinstead, or remap the prefix. If you're working over SSH on a remote VPS server, these local terminal conflicts can get even more confusing — keep:redoin your back pocket. - You're running real vi, not Vim. Classic
vioffers single-level undo —utoggles the last change and there's noCtrl-r, no undo tree. Runvim --versionto confirm what you're actually using. Need to select text instead? Here's how to select all in Vi/Vim. - Undo history vanished after closing the file. Expected behaviour unless
undofileis enabled.
📊 Vim Undo/Redo Commands Cheat Sheet
| Command | What it does |
u |
Undo one change |
3u |
Undo three changes |
U |
Undo all recent changes on the current line |
Ctrl-r |
Redo one undone change |
4Ctrl-r |
Redo four changes |
:undo N |
Jump to change number N |
:undolist |
Show the undo history table |
:earlier 5m |
Buffer state from five minutes ago |
:later 1f |
Move forward one file save |
. |
Repeat the last change (not a redo) |
Want the rest of the essentials? Check our Vim editor cheat sheet and the full list of top Linux Vim commands.
🔌 Useful Vim Plugins
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.
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 the Vim undo and redo process and everything you should know. Undo in Vim is one keystroke — u. Redo is Ctrl-r. That's the core. But beyond that, Vim gives you a full undo tree, time-based navigation with :earlier and :later, and persistent undo history with undofile. Learn these ten commands properly and you'll never lose work in Vim again. Good luck.
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'.