Vim loses undo history when changing buffers

Vim

Vim Problem Overview


If I'm working in a file, change to another buffer, and then change back, I have lost my undo history.

  1. vim File1.txt - make a bunch of changes & save.
  2. Open new buffer - :e test.txt
  3. Switch back to File1.txt - :b#
  4. Undo history is gone.

Any workarounds for this?

Vim Solutions


Solution 1 - Vim

You could :set hidden. This means that the buffer of the old file will only be hidden when you switch to the new file. When you switch back, you still have your undo history.

Solution 2 - Vim

You can also add persistent undo, this will have vim store your undo even through restart:

" Persistent undo
set undofile
set undodir=$HOME/.vim/undo

set undolevels=1000
set undoreload=10000

Edit - via @sanbor:

Don't forget to do mkdir ~/.vim/undo, otherwise vim won't do it for you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrianView Question on Stackoverflow
Solution 1 - VimRüdiger HankeView Answer on Stackoverflow
Solution 2 - VimAaron JensenView Answer on Stackoverflow