A lot of times, I end up accidentally closing a file with :wq when I only want to do :w. Since I work with multiple tabs and windows, the buffer is still there and I can open it with :ls followed by :b <number>. However, my previous changes are lost and I cannot undo anymore. Is there any way to set up vim so that it remembers the undo tree of unclosed buffers?

If that is not possible, a way to make vim confirm before quitting on :wq is also OK.


What is most likely happening in your case is that the buffer is automatically unloaded because you don't have the option 'hidden' enabled.

This option not only resolves the issue you reported, but is also required when you want to navigate between buffers without being forced to write the changes before doing so. Personally, I can't imagine working in Vim without it.


You can set a persistent undo with the following settings:

set undodir=~/.vim/undodir
set undofile

This is avaible in Vim 7.3 an above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.