62

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

It's an inconvenience to remembering to put an exclamation mark after w every time I want to save.

1
  • The answer would be different depending on the reason for the editor ending up in read-only mode.
    – Kusalananda
    Commented Apr 16, 2020 at 17:55

5 Answers 5

62

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.

4
  • 3
    how to reverse this and go back to read only?
    – Louis Hong
    Commented Nov 5, 2016 at 19:31
  • 5
    @LouisHong Very easily: :set ro. Commented Nov 14, 2016 at 9:58
  • 1
    This is perfect for if you open a writable file with view but then decide to edit it.
    – wisbucky
    Commented Mar 1, 2017 at 23:14
  • The reverse can be useful too. For example, psql has a meta command (\ev) to edit a SQL view, which opens vim to write. But, if I want to make sure I don't actually change the view (in production!) I can immediately :set ro. :) Commented Jan 27, 2020 at 20:01
12

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!
3

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.

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).

2

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

2
  • Note that the user finds it inconvenient to have to type an extra exclamation point, which makes me wonder whether your command would make it easier for them.
    – Kusalananda
    Commented Apr 6 at 9:35
  • Also note that this would save the file as the root user, which may not be what's intended.
    – Kusalananda
    Commented Apr 6 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .