60

I opened a file in readonly mode; is there a way to get out of readonly mode?

wrongusername
  • 1,027
  • 3
  • 11
  • 17
  • What are the limitation in readonly mode that are bothering you? If it's just that you want to write the file, even though you opend it read only, then adding a`!` to the `w` command will do the trick, as Michael stated. – Bananguin Apr 04 '13 at 11:59
  • 1
    @user1129682 it's just the inconvenience of remembering to put an exclamation mark after `w` every time I want to save. heh – wrongusername Apr 04 '13 at 20:01
  • The answer would be _different_ depending on the reason for the editor ending up in read-only mode. – Kusalananda Apr 16 '20 at 17:55

7 Answers7

60

You could do this:

:set noro

That unsets the read-only flag, but if the underlying file is still not writable by you then vim still will be unable to write to it.

Philip Kearns
  • 747
  • 4
  • 6
13

You can run chmod from within vim:

:!chmod +w %

! means run a shell command, and % is the current filename. You can also just force the file write:

:w!
Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
4

In addition to Michael Mrozek's answer, you can add a line to your .vimrc that allows you to write to a file that you have neglected to open with elevated permissions:

" Allows writing to files with root priviledges
cmap w!! w !sudo tee % > /dev/null

If the file is read only, you have only to enter :w!!, you will be prompted for your password and then the file will be successfully written to.

jasonwryan
  • 71,734
  • 34
  • 193
  • 226
3

While :set noro does the job, it doesn't check if the file is opened by another vim instance or updates the file if changed externally.

In order to make it editable and check for swap files (which is the default opening a file with vim) just use the edit command (:help edit):

:e

Note, if the file has been manipulated ever since (even outside of vim), it will update the changes in the current buffer (which I find normally desirable).

3

Here I go, although a bit late, maybe you already solved your doubt ;) I haven't seen in any of the comments a way I know with vim, so, I add it:

Once you are editing a file, you press :w or :wq, and you see the annoying message "E45 'readonly' option is set (add ! to override)"

E45 readonly option is set (add ! to override)

You can type

:w !sudo tee %

w !sudo tee %

  • w writes the buffer,
  • !sudo calls the shell with sudo,
  • tee redirects the output of vim :w to the output with tee, and
  • % is the current filename

And that should do the trick. Note that this will prompt to reload the file in vim, for what you have to press L

enter image description here

xCovelus
  • 205
  • 2
  • 11
-2

Try this one:

vim -R "filename" 

it open in read only mode, then just do this command :q

I tried and it worked for me

elbarna
  • 12,050
  • 22
  • 92
  • 170
-3

Try using sudo. As root you need to open the file. Sudo is a command where you can request permission from linux.

eg.

sudo vim "filename" 
SealsRock12
  • 67
  • 12
  • 1
    This has nothing to do with permissions or privileges. In addition, you should never use `sudo` to run an editor; use `sudoedit` instead. – Stephen Kitt Apr 16 '20 at 17:49