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

85,581

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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
Share:
85,581

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 1 year

    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?

    • Admin
      Admin almost 10 years
      Can you post result when running ls .viminf*?
    • Admin
      Admin almost 10 years
      What are the permissions on the viminfo file? What is the output of ls -la .vininf*?
  • yorkshiredev
    yorkshiredev about 9 years
    I'm afraid the question has very little to do with the creation of home directories...
  • yuansi zhu
    yuansi zhu about 9 years
    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.
  • yorkshiredev
    yorkshiredev about 9 years
    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". :)
  • nominalize
    nominalize about 9 years
    +1 In my case, this error occurred exactly because I was editing a file as a user which didn't have a home directory.
  • Smiley
    Smiley over 7 years
    Please note that HOME is a predefined environment variable and re-defining it is usually not a good idea.
  • Atul Vekariya
    Atul Vekariya almost 7 years
    Can you please clarify your answer. Oneline answers do not count as good in SE
  • Jeff Schaller
    Jeff Schaller almost 7 years
    Odd that adding other write permissions helped you. Perhaps you didn't own your own file?
  • bballdave025
    bballdave025 over 4 years
    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.
  • Ned64
    Ned64 over 4 years
    That is one possibility, yes.