I have been trying to get persistent undo to work for over a day now. I keep having a quite unusual bug. I tried the following:

" Let's save undo info!
if !isdirectory($HOME."/.vim")
    call mkdir($HOME."/.vim", "", 0770)
if !isdirectory($HOME."/.vim/undo-dir")
    call mkdir($HOME."/.vim/undo-dir", "", 0700)
set undodir=~/.vim/undo-dir
set undofile

I then open an existing file make a few edits and close the file. Open the file and type 'u' - The entire buffer is emptied.

Then looking through the documentation I found this snippet:

au BufReadPost * call ReadUndo()
au BufWritePost * call WriteUndo()
func ReadUndo()
  if filereadable(expand('%:h') . '/UNDO/' . expand('%:t'))
    rundo %:h/UNDO/%:t
func WriteUndo()
  let dirname = expand('%:h') . '/UNDO'
  if !isdirectory(dirname)
    call mkdir(dirname)
  wundo %:h/UNDO/%:t

This seems to work better, but I am still getting an undo at the beginning of every file that removes all content (i.e. pressing u to many times results in an empty file no matter what I started with) - is there a way to avoid this?

My version info:

NVIM v0.4.3
Build type: Release
LuaJIT 2.0.5
Compilation: /usr/bin/cc -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -O2 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -I/build/neovim/src/build/config -I/build/neovim/src/neovim-0.4.3/src -I/usr/include -I/build/neovim/src/build/src/nvim/auto -I/build/neovim/src/build/include
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"
  • 1
    Welcome to Vi and Vim! Can you edit your Q and give more detail as to what exactly is happening? e.g., you edit a file, save-and-quit, and when you edit again, pressing u does what exactly? FWIW, in my vimrc I use (the linebreak-ed equivalent of) set undofile undodir=~/.undo if !isdirectory(expand(&undodir)) call mkdir(expand(&undodir), "p") endif
    – D. Ben Knoble
    Jan 5, 2020 at 17:38
  • Was I not clear it empties my file?
    – Jim Hessin
    Jan 5, 2020 at 21:24
  • 1
    I was giving an example. The sequence of steps, the results, and the expected results are somewhat lacking in clarity, yes.
    – D. Ben Knoble
    Jan 5, 2020 at 21:25
  • @d-ben-knoble I tried this: set undofile undodir=~/.undo if !isdirectory(expand(&undodir)) call mkdir(expand(&undodir), "p") endif But it resulted in the same result as my first example (deletes the buffer when trying to undo anything)
    – Jim Hessin
    Jan 5, 2020 at 23:59

1 Answer 1


I found what the problem was. It was caused by a plugin:


I'm not sure why it causes such strange behavior, but I have uninstalled it and now my undo history works fine.

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.