Persistent undo and repeat
Like any editor, Vim keeps track of every operation. Press u to undo a last operation, and Ctrl + r to redo it.
Undo tree
To learn more about Vim’s undo tree (Vim’s undo history is not linear) and how to navigate it, see Chapter 4.
Vim also allows you to persist undo history between sessions, which is great if you want to undo (or remember) something you’ve done a few days ago!
You can enable persistent undo by adding the following line to your .vimrc
:
set undofile
However, this will litter your system with an undo file for each file you’re editing. You can consolidate the undo files in a single directory, as seen in the following example:
" Set up persistent undo across all files. set undofile let my_undo_dir = expand('$HOME/.vim/undodir') if !isdirectory(my_undo_dir)) call mkdir(my_undo_dir, "p") endif set undodir=my_undo_dir
For Windows users
If you’re using Windows, replace the directories with $USERPROFILE\vimfiles\undodir
(and you’ll be making changes to _vimrc
instead of .vimrc
).
Now, you’ll be able to undo and redo your changes across sessions.