1

Are there any permission settings that need set on a file to get persistent undo to work? I close and open my vimrc file and I can keep hitting the u key to see my previous edits. But it does not work for production code files. Any ideas what could be the issue?

Edited to Add:

Here is a sample of my .vimrc:

" Turn persistent undo on. This means that you can undo even when you close a
" buffer/VIM.
if has("persistent_undo")
  if isdirectory(expand("~/.vim/temp_dirs/undodir"))
    set undodir=~/.vim/temp_dirs/undodir
  elseif isdirectory(expand("~/vimfiles/temp_dirs/undodir"))
    set undodir=~/vimfiles/temp_dirs/undodir
  else
    echomsg "ERROR: undodir does not exist or is not writable."
  endif

  set undofile
endif

My .vimrc has 644 permission and is owned by snrub:snrub. An example of a file that does not have persistent undo would be /var/www/html/sites/all/modules/custom/mymodule/mymodule.module (this is a Drupal 7 installation).

This file has 664 permission and is owned by apache:apache. User snrub is a member of the apache group, so I do have write permission on the file.

verbose set undodir gives the following:

undodir=~/.vim/temp_dirs/undodir
    Last set from ~/.vimrc

...which is a directory that does exist.

3
  • 1
    I think the viminfo file is responsible for persistent undo. Also the undo setting (or possibly undodir; i dont remember)
    – D. Ben Knoble
    Commented Nov 29, 2019 at 20:44
  • 2
    Make sure you set undodir
    – eyal karni
    Commented Nov 29, 2019 at 21:40
  • 1
    The 'undodir' setting is probably the answer to this question... But unfortunately it's too vague right now to be able to offer a good answer. How do you set the undo variables? (In other words, how do you enable persistent undo)? You mentioned the "production" code files, what is different about those in particular (in your environment) that could affect the behavior of undo? If you manage to ask a more specific question, you'll be likely to get a great answer.
    – filbranden
    Commented Nov 29, 2019 at 21:43

2 Answers 2

0

After trying out many different ways of opening and editing the file, it appears the issue is that with the "production code" (.module) file, the user snrub was not the owner of the file. I guess in order for persistent undo to work, the user editing the file must be the owner. And chmod file permissions are irrelevant.

Works:

vim filename.module for file owned by snrub:snrub

sudo vim filename.module for file owned by root:root

Does not work:

vim filename.module for file owned by apache:apache

sudo vim filename.module for file owned by apache:apache

-2

Persistent undo is controlled by :h 'undofile' option, it's off by default.

If it's on, vim store undo history under :h 'undodir' when writing a buffer to a file, and restore undo history from the same file on buffer read.

Open your vimrc, and execute

verbose set undofile?

It should tell you where it was set last time.


Personal opinion about persistent undo:

Not sure if it's a good idea to turn on 'undofile', if it's off, you can hold u or execute :undo 0 to undo all the changes made in current session, if it's on, you have to use :earlier 1h family command instead. It also cost more and more memory as your undo history getting bigger and bigger.

Personally I prefer manual style undo history read and write:

set undodir=$HOME/.vimundo//

command WriteUndo setlocal undofile | w | setlocal noundofile
command ReadUndo setlocal undofile | e | setlocal noundofile

You can also use :h :rundo and :h :wundo to manually read and write undo history if you prefer not to store the undo history file in undodir.

Update to address OP's update

It's a bit surprise that no error would occur if undodir doesn't exist.

You setting is over complicated, AFAIK, persistant-undo was added in vim7.3, that's long long long ago, I also don't see the point of checking multiple undo directories, you might want to simplify it to

set undofile undodir=~/.vim/temp_dirs/undodir
if !isdirectory(&undodir)
  call system('mkdir -p ' . &undodir)
endif
8
  • 1
    Well I like to have my .vimrc file work with both Windows and Linux, since I use both on a regular basis. So I test for both .vim (Linux) and vimfiles (Windows) directories.
    – MrSnrub
    Commented Nov 30, 2019 at 12:53
  • @MrSnrub It's possible to set undodir to multiple directories, first found one will be used. I'm not familar with windows vim setting, maybe you can use ~/.vimundo as undodir for both windows and linux ?
    – dedowsdi
    Commented Nov 30, 2019 at 13:06
  • 4
    I don't think your reservations about persistent undo are warranted. I've been using it trouble-free for years. I never clean the 'undodir' yet on my primary Win box there's only 28 MB in there from a total of 500 files...largest is 2MB...memory usage from this is never a concern. And that's with an 'undolevels' cap of 3000...three times the default value.
    – B Layer
    Commented Dec 1, 2019 at 12:12
  • 1
    @BLayer Thanks, I decide I to give it a shot.
    – dedowsdi
    Commented Dec 1, 2019 at 13:27
  • 1
    Cool. Hope everything works well. You might want to check out undotree. Depending on how you interact with the history it can be quite convenient.
    – B Layer
    Commented Dec 1, 2019 at 13:44

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.