Categorized under: command line, linux, mac, programming, Ubuntu, vi, vim

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.
001-b_open_read_only

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

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:
003-b_try-to-save

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:
005-b_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”.
006-b_final-save

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.

Comments

  1. Perfect, thanks for the post!


    John
    September 8th, 2013
  2. Thanks, very helpful.


    Mohamed
    December 20th, 2013
  3. Thanks!!


    Rich
    January 24th, 2014
  4. Thank you for this!! Too many face palm! This problem has been driving me up the wall until I found your post =D


    Xzuku
    February 4th, 2014
  5. GREAT!!!!

    Saved me a ton of time, Thanks…


    James
    February 5th, 2014
  6. Awesome!! This was very irritating .
    Thanks for the solution.


    Manish
    March 15th, 2014
  7. Really great explanation! Saved my sanity, thanks


    chris jarvis
    March 19th, 2014
  8. It was definitely helpful. I am so glad it worked!


    Abhishek Roy
    June 19th, 2014
  9. lifesaver ;-)


    Michael
    June 23rd, 2014
  10. Thanks, I am very green when it comes to command line Linux. I’m glad I found this page.


    Ryan
    October 29th, 2014
  11. 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


    Brandon Doyle
    November 1st, 2014

RSSSubscribe to my feed now.

About Me

Adam Culp (GeekyBoy)
Adam Culp (GeekyBoy) I am a South Florida PHP developer passionate about technology and post things here I find interesting, mostly so I remember, but also to help others. I'm a PHP 5.3 certified engineer and serve on the Zend Certification Advisory Board, and currently work as a Senior Professional Services Consultant with Zend Technologies. I am very active in the national and local PHP community and organize the South Florida PHP Users Group (SoFloPHP), as well as the SunshinePHP Developer Conference in Miami. Read More >>
Zend PHP 5.3 Certified Zend Certification Advisory Board
Pair with me

profile for Adam Culp at Stack Overflow, Q&A for professional and enthusiast programmers