Reputation: 14270
I'm editing files using Vim 8.1 on macOS Mojave and I've noticed recently that when I open various files, make a change, and then try to save the file, Vim says
"E45: 'readonly' option is set (add ! to override)"
when I already have read and write permissions on the file.
I checked each file's permissions and I do own them and the permissions are always either "-rwx-r-xr-x
" or "-rw-r-r-
".
Exiting Vim and running chmod
to rewrite the file permissions doesn't fix the problem. The only way to fix it is to do ":w!
" inside Vim.
What are the possible reasons this is happening?
Upvotes: 4
Views: 9569
Reputation: 2196
This so simple, Just try the below command:
:w !sudo tee %
Explanation, What's happening?
:w – write
!sudo – call shell sudo command
tee – the output of write (:w) command is redirected using tee
% – current file name
So, in next Press L to reload.
just this!!!
Upvotes: 1
Reputation: 996
This happened to me because I had followed the advice in this answer, which makes vim set readonly if a swapfile is present. The problem resolved when I followed the advice in this answer, which uses the SwapExists event.
Upvotes: 0
Reputation: 394
I've recently encountered a similar issue and while I'm unsure of the root cause, I ended up with some views with the readonly setting enabled. I have some autocommands when entering and leaving buffers to generate views, though I'm not sure what led to readonly getting set in these instances.
If you're automatically saving views, you can simply run :set noreadonly
while editing the file. You can also manually edit the view file for the corresponding file (you can find the folder where your views are saved by running :set vdir?
in vim command mode, and the view file should have a name corresponding to the path of the file) and remove the 'setlocal readonly' line. Alternatively you could also just delete the view file entirely if you don't want there to be a view.
I also had a similar issue with the root
user when I tried to configure their backup/swap/undo. I managed to get things working again by removing group and all user perms on those directories in the root user's home (leaving drwx------
on each of them).
Upvotes: 0
Reputation: 196
Regardless of what permissions you have on the file, there is an option in Vim to set the editor in readonly mode, forcing you to use :w!
to save. By the error you posted, that seems to be your case.
Take a look at the Vim help, using :help
, for both readonly and error 45 ("E45").
:help E45
'readonly' option is set (add ! to override)
You are trying to write a file that was marked as read-only. To write the
file anyway, either reset the 'readonly' option, or add a '!' character just
after the command you used.
:help readonly
'readonly' 'ro' boolean (default off)
local to buffer
If on, writes fail unless you use a '!'. Protects you from
accidentally overwriting a file. Default on when Vim is started
in read-only mode ("vim -R") or when the executable is called "view".
When using ":w!" the 'readonly' option is reset for the current
buffer, unless the 'Z' flag is in 'cpoptions'.
{not in Vi:} When using the ":view" command the 'readonly' option is
set for the newly edited buffer.
As the error says, your editor seem to have that flag on by default. You can check the following:
vim
command has an alias that is imposing the -R
flag.vimrc
configuration for any set ro
line that may be enabling that optionIf any of those is the case, you know what to do, remove the -R
from the alias and the set ro
line. You can always disable the option from the .vimrc
configuration, by adding a line set noro
.
Upvotes: 4