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”
Perfect, thanks for the post!
Thanks, very helpful.
Thanks!!
Thank you for this!! Too many face palm! This problem has been driving me up the wall until I found your post =D
GREAT!!!!
Saved me a ton of time, Thanks…
Awesome!! This was very irritating .
Thanks for the solution.
Really great explanation! Saved my sanity, thanks
It was definitely helpful. I am so glad it worked!
lifesaver 😉
Thanks, I am very green when it comes to command line Linux. I’m glad I found this page.
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
[…] Saving a read-only file edited in vi / vim | GeekyBoy.com. […]
this is awesome!!
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 😛
Thanks, saved me three times now. First result on Yahoo search makes it easy to get back here too.
Worked perfectly and explained perfectly. Thanks!
Awesome post man!
This trick is a life saver and will prevent many a face-palm!
Awesome! But we cannot use these steps if the read-only file is in mounted directory.
I got the trouble,too
thank for your advice
It’s work !!!
Genius!! Thank you
Awesome man.. many explained this method.. your explanation is clear.. with picture depiction.. Nice!!!
” 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. 🙂
Thanks, … very useful…
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 🙂
Fantastic solution
Thank you so much for this information! That’s great!
Changed my life.
Thanks…Really useful
I keep this command on a sticky, attached to my monitor.
One more thanks!
Love you!
Thanks by your advise
thanks bro!
Thanks so much!