How to Force-Save Changes to Read-Only Files in Vim
It has happened to me so many times that I edited a file such as /etc/hosts with a normal user and tried to save my changes but Vim won't allow me because the user has read-only rights to the file.
If you’ve already made changes to a read-only file, Vim will cling to you like it’s the ultimate gatekeeper. In such scenario, you don’t want to exit Vim without saving because you have spent time making the changes and you cannot write (save) your changes because Vim won’t allow for it.
Unless you know how execute commands from within vim to use sudo
in order to forcefully overwrite your changes.
If you don’t have sudo access, you can still do something as explained later in this post.
If You Have Sudo Privileges
Let’s break down how to still write your changes even if you opened a readonly file but you have sudo rights to the system.
I’ll try to edit the /etc/hosts
file with my normal user that does not have write access to this file.
The file belongs to root, but I’m logged in as my own user ‘rashid’, but let’s try to edit it anyway.
vim /etc/hosts
If you’re editing a readonly file, you will see an indication but this can easily be overlooked, and you may only realize it when you have done the work.
When you switch to insert mode, it will show a warning too, but on remote servers with no syntax highlighting, this is also easy to be overlooked:
Now, let’s make some changes to this file and write it with :wq
or :x
Notice the error about the readonly option being set:
Even if you try to force writing it with !wq,
it will still not work. You can only quit your changes with :q!
(force quit) but then your changes will not be saved.
To make sure your changes are saved first before you force quit, you can type the following command:
:w !sudo tee %
Let’s break this down:
:w
is the write command.In Vim, the “!” (bang) symbol, helps you execute external shell commands directly from within Vim.
!sudo
elevates your privileges usingsudo
.tee %
writes the contents of the current buffer (%
represents the current file) to the file, bypassing the read-only restriction.
After running this command, you may be prompted for your password, and the changes will be saved after you type the password.
Let’s give it a try:
The message displayed indicates that the file has been changed.
Will Vim Let You Go Now?
No, After using :w !sudo tee %
to save the changes, Vim still thinks the file is read-only because it hasn’t updated its internal buffer state.
But the file has been saved, so if you force quite the file with !q
, can try to see the content again, you will see our changes are saved.
Congrats! You made it out of Vim 💪👏😁
What If You Don’t Have Sudo Privileges?
There are other ways to save your changes - of course. The following method is the simplest of all.
The :w
command in Vim also can take a path as an argument to write the content to. As simple as that.
So if vim does not allow me to save my changes to the current file due to lack of permissions, I can save it to a different file, such as a new file in my home directory where I have full permissions. Later, I can copy my edits to the actual file or replace the files all together.
Closing
If Vim doesn’t want to allow you to write your changes because the user you open the file with does not have write permissions to the file, you can use the ! symbol to run external commands, in this case, a tee command to write to the file with sudo to escalate the privileges.
If you don’t have sudo rights, you can save your changes to a new file.
Thank you for reading Vimology’s Substack!!