Saving a read-only file edited in vi / vim

We’ve all done it…opened a file using vi or vim to inspect the contents, and realize we need to alter it.  Of course we totally ignored the message informing we didn’t have permission to edit, so we’re only allowed to view it as “read-only”.  Then after we find the troublesome spot we hit “i” and happily edit the place needing changed, only to “face-palm” when we realize we cannot save the wonderful edit we just made.

In the past I handled this one of three ways: I either copied and pasted the change after reopening the file using sudo, or I reopened and retyped everything once again, or I save the file as a temp file and then rename it using sudo.  Very stupid, stressful and time consuming.

However, now I know a better way.  Using a combination of ‘tee’ and ‘sudo’ commands I can now save the read-only file rather than jumping through the hoops in the previous paragraph.  Here is how:

Open a file as normal, forgetting to use “sudo”, and therefore viewing a read-only file.

Then mistakenly try to edit the read-only file in the traditional manner.

But when we try to save using ‘:w!’, SHIFT+ZZ, or :qw!, or whatever combination we normally fail with as an alternative.  Here is sample output of what we see:

At this point is where the new magic can happen. Instead of the normal “face-palm” we do not “ENTER” and move on. We can enter a new command and successfully save the file after entering the sudo password:

At this point we will be presented with the content of the file and a prompt to press ENTER or type another command. To simply save the file and move on we just press ENTER, and then press the letter “O” (oh). (NOTE: “L” seems to do pretty much the same thing.)  The file will be saved but remains open in vi/vim for more editing or reading.  We can now exit normally by typing “:q!” since the file is still open as “read-only”.

What the command does:

  • :w = Write a file.
  • !sudo = Call shell sudo command.
  • tee = The output of the vi/vim write command is redirected using tee.
  • % = Triggers the use of the current filename.
  • Simply put, the ‘tee’ command is run as sudo and follows the vi/vim command on the current filename given.

33 responses to “Saving a read-only file edited in vi / vim”

  1. Awesome! This worked for me. I had to edit a file on my Raspberry Pi and I couldn’t save it. I’ve come across this before though — exactly as you described in the opening paragraphs, and it drove me crazy because most of the time I couldn’t simply copy and paste it back in after using sudo.

    Thanks! I’ll post a link to this on my site!

    Brandon Doyle

  2. Best post ever, but I am fraid that found this just with a lot of luck, because i also working on default file of apache2, so the example is the same what i want to to even with the same file 😛

  3. Awesome man.. many explained this method.. your explanation is clear.. with picture depiction.. Nice!!!

  4. ” To be able to save/write (to) read-only files easily (To use this command, type :Sudow) – Note that all custom commands need to start with an uppercase letter – Also note that % refers to the current file/buffer and >/dev/null re-directs the output from tee
    command Sudow w !sudo tee % >/dev/null

    Note: Put this shortcut in your .vimrc to make using it quicker and easier. 🙂

  5. Thanks for not letting me copy and paste the command in the article. After typing it a few times, I remember it by heart now 🙂