Persistent Undo in Vim

about a 2 minute

This post presents a chapter from my upcoming book Mastering Vim Quickly: From WTF to OMG in no time

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.

 

Persistent Unto in Vim

Persistent Undo in Vim

 

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.

 

 


If you want to get notified when I publish the book or get more content from the book for free, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.


If you liked the post, we should get connected - follow me on Twitter

1 Comment Persistent Undo in Vim

  1. Pingback: Persistent Undo in Vim – Full-Stack Feed

Leave a Reply

Your email address will not be published. Required fields are marked *