9

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.

2 Answers 2

3

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.

12

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

1
  • Having persistent undo set just saved my life. It creates an undo tree, with each branch a different course of edits to different versions that would have been lost if persistent undo was set. Undotree provides a convenient interface to it. I would thoroughly recommend having both set.
    – markling
    Commented May 12 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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