When I :q vim on my debian root account (not even writing), it always displays an error message:

E138: Can't write viminfo file $HOME/.viminfo!

.viminfo didn't exist, so I created it and checked the permissions of /root and the file, but they were fine:

drwx------  3 root root 4.0K Sep 16 11:13 .
-rw-r--r-- 1 root root 0 Sep 16 11:13 /root/.viminfo

$HOME exists as an environment variable:

~ # echo $HOME

in vim :!echo $HOME returns empty output. How can I get rid off this error, why isn't vim accepting $HOME?

  • Try run vim like this: env HOME="/root" vim --noplugin.
    – tivn
    Sep 16, 2018 at 13:50
  • It works, the error is gone but what's the reason that it ignores $HOME? $PATH is accessible in vim for some reason. Sep 16, 2018 at 15:07
  • Now try with vim --noplugin. Then env HOME="/root" vim.
    – tivn
    Sep 16, 2018 at 15:44
  • I see what you did there, works without plugins. So I removed my /etc/vim/vimrc.local global config but still get same error. Okay, it's a probably not a vim issue, right? Sep 16, 2018 at 20:18
  • Do you have same issue when you run vim as non-root? If this is only happen with root user, then it is very likely the issue is coming from /root/.vim directory rather than under /etc or /usr/share/vim. Try grep -r HOME /root/.vim /root/.vimrc .
    – tivn
    Sep 17, 2018 at 2:27

3 Answers 3


Logging in and out of the shell solved it for me after all else failed.

Defining let $HOME="/root" in .vimrc does not help as it can not find $HOME directory to read the .vimrc if :!echo $HOME shows nothing, i.e. is empty. ;-)


I guess this is some kind of weird bug in the vim stable release, since I installed vim from testing and it just works without further ado.


this solution works for me

just press enter when this message appears, and reopen the file like sudo vi file.sh and it's done, you can modify and quit the file as you want

just matter of permission issue to viminfo file

  • 1
    If the problem comes from the permissions of the file, using chmod and/or chown might be a cleaner option than using vim with sudo rights.
    – statox
    Jun 21, 2019 at 14:42
  • 1
    sudoedit is generally a better solution than sudo vi
    – D. Ben Knoble
    Jun 21, 2019 at 15:12

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.