Vim Error E138: Can't write viminfo file $HOME/.viminfo!
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
Related videos on Youtube
Gilles 'SO- stop being evil'
Updated on September 18, 2022Comments
-
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 uponsudo vim README.txt
, the file renders blank, and gives me aE138: 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 almost 10 yearsCan you post result when running
ls .viminf*
? -
Admin almost 10 yearsWhat are the permissions on the viminfo file? What is the output of
ls -la .vininf*
?
-
-
yorkshiredev about 9 yearsI'm afraid the question has very little to do with the creation of home directories...
-
yuansi zhu about 9 yearsYou 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 about 9 yearsThere 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 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 over 7 yearsPlease note that
HOME
is a predefined environment variable and re-defining it is usually not a good idea. -
Atul Vekariya almost 7 yearsCan you please clarify your answer. Oneline answers do not count as good in SE
-
Jeff Schaller almost 7 yearsOdd that adding other write permissions helped you. Perhaps you didn't own your own file?
-
bballdave025 over 4 yearsI 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 over 4 yearsThat is one possibility, yes.