I'm afraid I've run into something rather strange. When I open a file normally, vim README.txt, everything is fine. But upon sudo vim README.txt, the file renders blank, and gives me a E138: Can't write viminfo file $HOME/.viminfo! error upon trying to exit.

I suspected the .viminfo file was corrupt, so I deleted it. This problem remains. Can anyone help?

  • Can you post result when running ls .viminf*?
    – cuonglm
    Jun 4, 2014 at 16:41
  • What are the permissions on the viminfo file? What is the output of ls -la .vininf*?
    – darnir
    Jun 4, 2014 at 16:42

11 Answers 11


When you run sudo vim you start vim as root. That means that it is the viminfo file in /root that is the problem. You should do rm /root/.viminf*.

To make sure of this, run sudo vim and execute this command: :!echo $HOME. This will show you that your home directory is /root.

I would recommend that you do not run vim as root, but rather use sudoedit. This is a more secure solution as the editor is not running as root. You never know what a plugin might do. Additionally it allows you to use your own settings and plugins in vim and not the ones in roots vimrc. sudoedit is the same as running sudo -e. sudoedit works by making a temporary copy of the file that is owned by the invoking user (you). When you finish editing, the changes are written to the actual file and the temporary file is deleted.

As a general rule of thumb: Do not run things as root if it is not necessary.

  • I think you might have saved me from a bunch of worry with the note, "This is a more secure solution as the editor is not running as root. You never know what a plugin might do." I believed that I had thought of all vulnerabilities, but I was wrong. Aug 29, 2019 at 22:27

I got this error on every exit. I did not use sudo. It explicitly mentioned my user home directory:

E138: Can't write viminfo file /Users/henrik/.viminfo!

Removing ~/.viminfo did not fix the error.

Turns out I had a bunch of viminfo temp files, and removing those fixed the issue:

 ls ~/.viminf*      # If you want to see the files.
 rm -rf ~/.viminf*  # Remove them.

Found this solution here.

  • 1
    I'd suggest using rm ~/.viminf*.tmp - no forcing and no subdirs, to be on the safe side.
    – einpoklum
    Feb 22, 2023 at 12:45

I received the same error when trying to :wq a file on a disk that was completely full. If you receive this message, you may wish to check your available disk space.


For me the issue was that I had changed my home directory to location inside an NFS mount. For some reason, this resulted in the owner of the directory to be "nobody". Adding write permissions for "other" solved the problem as I was clearly not the owner of the file.

chmod o+w ~/.viminfo
  • 1
    Can you please clarify your answer. Oneline answers do not count as good in SE Jul 30, 2017 at 10:04
  • 2
    Odd that adding other write permissions helped you. Perhaps you didn't own your own file?
    – Jeff Schaller
    Jul 30, 2017 at 12:01
  • This works for me. I guess my ".viminfo" was created by user "root" and if user "ubuntu" tries to modify it, he needs a write permission. In a nutshell, "-rw-r--r--" becomes "-rw-r--rw-". Jun 15, 2019 at 11:54

A much more cleaner way to create a home directory would be to use the skeleton that linux provides for you for example.

sudo cp -a /etc/skel /home/usernmae && sudo chown -R usernmae:usernmae /home/usernmae
  • 2
    I'm afraid the question has very little to do with the creation of home directories... Feb 2, 2015 at 23:49
  • You need to create a home directory in order for vim to be able to save to the .viminfo file, so yes the above command is applicable.
    – Rick
    Feb 3, 2015 at 2:49
  • 3
    There are countless reasons why this file wouldn't exist, before you can reach such a conclusion. The absence of a home directory would probably cause much more trouble than just a missing file ; with this reason I could answer pretty any "missing file" question with "create your home directory". :) Feb 3, 2015 at 17:26
  • 1
    +1 In my case, this error occurred exactly because I was editing a file as a user which didn't have a home directory.
    – Zoltán
    Apr 12, 2015 at 16:35

It seems like you have cleared environment variables.

Do this:

export HOME=/root

This should resolve your issue.


Most likely this is happening because the home dir has run out of disk space and any new vim operation is unable to write in the .viminfo file.

  • 3
    That seems a bit farfetched: do you have any evidence for that?
    – jasonwryan
    Nov 12, 2015 at 3:18
  • That is one possibility, yes.
    – Ned64
    Oct 23, 2019 at 13:43

I had the same issue when updating the content of Makefile. I needed a local variable and decided to name it HOME, so I entered the line HOME=2.154. This causes the vim-HOME setting to be replaced by a non-existing directory. Don't use the HOME variable for other use.

  • 1
    Please note that HOME is a predefined environment variable and re-defining it is usually not a good idea.
    – Barun
    Sep 26, 2016 at 7:28

I think this may only apply to pretty old versions of linux/vi but what I found was that there were temporary files in the root's home directory with the filenames /root/.viminfa.tmp ... /root/.viminfz.tmp and that there were 26 of them with timestamps covering several years. We had run out of letters of the alphabet as all of a-z were taken. I removed all of those files and the problem dissappeared. Of course Henrik's # rm -rf ~/.viminf* would fix this problem just fine.


Not sure how I came across this scenario myself as I don't recall opening any files using sudo vim but on each :q of a file I was getting a similar 'Can't write viminfo is not writeable'.

rm ~/.viminfo didn't resolve it and ~/.vim/ was empty.

chmod 666 ~/.viminfo resolved that warning message when exiting a file in Vim. Although I'm not certain this is the best solution? It makes sense why it stopped the warning since .viminfo was rw------ before and I assume is getting written to with vim command histories.


I my scenario, it was the absence of /root/.viminfo file altogether. After I touched the file, the error stopped and the file was updated up on exiting vim. PS : This is windriver embedded linux.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .