In undo – redo I’ve described how to undo and redo changes in a file. By default, the undo stack is deleted when you close a file. However, this can be changed and also the size of the undo stack can be controlled.
There are three interesting options which you can use:
- :set undolevels=1000
The undolevels setting has a default of 1000 on my system. It controls how many changes are remembered and thus can be undone.
Use :set undolevels without a value in command mode to check the current value on your system. - :set undodir=~/.vim/undo/
By default, the undo information is saved in the same directory as the file which is edited. The undo file is a hidden and has an extension of.un~
. I prefer to have all these files in a separate place somewhere in my home directory. Therefor I create the~/.vim/undo/
directory and configure vim to save the undo information of all my edits there. - :set undofile
This is the switch which actually enables saving change information to a file and thus make it persistent across multiple edit sessions.
If the undo file can not be written, you will not get any warning unless you explicitly :set verbose=1.
So the section in your .vimrc
file to persistently record the last 1000 changes for each file you edit could look like this:
:set undolevels=1000 " default is 1000 :set undodir=~/.vim/undo/ " set the directory to store undo files :set undofile " activate undo info saving " ATTENTION: vim does not delete these fiels, you " need to delete them manually
That all said, this is a configuration which I actually do not use. Why not? I often open a file, make some changes to it, leave the editor open and test the changes. If I’m not happy with the result, I want to be able to go back to the version of the file before I opened it for editing. Without the configuration presented here, I can simply use the undo command until there is no more undo possible and then I know I’m back at the original version (unless I made more than 1000 individual changes). But with persistent undo, I might undo a change I did during my last edit session, which could be weeks or months back. That is not what I want.