~ 1 minute
As you already saw in the chapter on Undo/redo, Vim is pretty powerful when it comes to these features. However, there’s one more feature which I didn’t mention, as it takes a bit of configuration.
In Vim, like in every other text editor, you can perform undo/redo in your current session. Once the session is closed, and you reopen the same file, running undo will do nothing – as you will be already at the oldest change.
Vim supports persistent undo, which means that you can run undo/redo even from your previous sessions.
This is great feature indeed. This way you can go back historically through changes of any of your files.
How this works? Vim creates hidden file where it stores the undo history, for every file you edit. Now, configuration is very simple. You could add only this line to your .vimrc
:
set undofile " Maintain undo history between sessions
and it would work.
However, Vim will write undo history files in the same directory as the file you edit. Overtime, this will become messy. You don’t want that. That’s why, I recommend you complete the next two steps as well.
First, create a dedicated directory for these undo history files, by running a command like:
$ mkdir ~/.vim/undodir
My assumption is that ~/.vim
is your Vim directory, where your .vimrc
, among others, is located.
Then, once you have created the directory, you need to add only one more line to your .vimrc
file:
set undodir=~/.vim/undodir
That’s all. Vim will store all the undo history files in that directory, and you’ll have persistent undo working flawlessly.
Would you like to get 4 chapters from Mastering Vim Quickly for Free? Sign up below:
If you liked the post, we should get connected - follow me on Twitter
Pingback: Persistent Undo in Vim – Full-Stack Feed
Pingback: Persistent Undo in Vim – Full-Stack Feed
Cool post, thanks.
I didn’t know how much I needed this until I saw this on Twitter. Thanks for sharing.
Thanks, it was a really useful tips :)