Is there way to prevent Vim from clearing history after exiting Vim?

I want to be able to see the changes I made, while I am using Gundo (or UndoTree), even though I quit vim and the terminal and come back to the same file later on?


Use undotree setting for this - :h undotree.txt, :h undodir and :h presistent_undo.

I have it like so:

if has('persistent_undo')
  if exists('$SUDO_USER')
    " don't create root-owned files
    set noundofile
    set undodir=~/.local/tmp/vim/undo//
    " keep undo files out of the way
    set undodir+=~/.vim/tmp/undo//
    set undodir+=.    
    " actually use undo files
    set undofile

Despite the check for sudo user to not create a undo file owned by root, this sets few dirs for Vim to create undofile (first available will be used). Also this has set undofile to make sure undofile will be used.

I just checked on some config file with UndoTree and there are changes made 11 days ago and I'm sure I did reboot my laptop in between at least 4 times.

| improve this answer | |
  • Thanks a lot @grodzik! Do you know how long it keeps the undofile? – ogirginc Jan 20 '17 at 14:48
  • I would guess that forever - it says persistent ;) – grodzik Jan 20 '17 at 15:04
  • That makes sense! 😁 – ogirginc Jan 23 '17 at 18:23
  • FYI: The code @grodzik provided works both for UndoTree and Gundo. – ogirginc Feb 8 '17 at 12:47

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.