How many times have you had a situation when you open a file for editing, make a bunch of changes, and discover that you don't have the rights to write the file? This happens to me a lot.

It usually goes like this. You open a file and you forget to use sudo:

$ vim /etc/apache/httpd.conf

You make many changes and then you type:


And you get an error:

"/etc/apache/httpd.conf" E212: Can't open file for writing
Press ENTER or type command to continue

And then you go like, duh. At this point you either quit vim:


And open the file again with sudo:

$ sudo vim /etc/apache/httpd.conf

And make all the changes again. Or if you're a bit smarter, you save the file to /tmp directory:

:w /tmp/foo

And then you sudo move the /tmp/foo to the right location:

$ sudo mv /tmp/foo /etc/apache/httpd.conf

Don't do that anymore! Use this command:

:w !sudo tee % >/dev/null

This command will save you hundreds of hours throughout your career. Here's how it works - vim spawns sudo tee FILENAME and pipes the contents of the file to its stdin. The tee command now runs in a privileged environment and redirects its stdin to FILENAME. The >/dev/null discards tee's stdout as you don't need to see it.

In fact, don't use this command as it's too too long and complicated to remember! Save another few hundred hours and create a vim alias for this command because you'll use it for the rest of your life. Put this in your ~/.vimrc:

cnoremap sudow w !sudo tee % >/dev/null

Now the next time you're in this situation, just type:


See you!


Trey Permalink
March 09, 2014, 18:40

Why not use cat instead of tee?

March 09, 2014, 19:01

Because cat only produces output to stdout, and redirections are setup before the command is run in privileged environment.

March 09, 2014, 19:10

If you use cat, the command would need to be "!sudo cat > %"

The redirection is what writes the file, but that runs as the calling shell, only the cat command is being run by the superuser, so it will fail to write the file.

March 09, 2014, 19:17


Joey Hewitt Permalink
March 09, 2014, 23:27

Here's one way to get the redirection to happen in a priveleged shell:

:w !sudo sh -c "cat > %"

Warning though, this may need some escaping to ensure strange characters in the filename are passed through correctly. Does the tee solution need escaping?

Also, IMO :cmap is not a good fit for this. Now if I want to search for the string "sudow" in my buffer (by typing "/sudow") it gets expanded. It is also strange in that whenever you type an "su" in command mode, there is a delay to see if the mapping is being used. (Workaround is to insert a pause in your typing so that vim knows you are not using the mapping -- see "timeout" and "timeoutlen" options.)

Igor Mikushkin's solution works for me. The one annoyance is that user-defined commands such as this have to start with an uppercase letter.

Anyway, I have also wrestled with this problem of editing protected files, so it's good to have a solution, thanks!

December 14, 2014, 07:47

Nice post
Merry christmas

December 08, 2014, 10:03

This article is simple awesome which provides extra knowledge also.Thanks for sharing such a great post with us.

Merry Christmas 2014 Quotes


Igor Mikushkin Permalink
March 09, 2014, 20:31

This does not work for me:
cnoremap sudow w !sudo tee % >/dev/null

But this one works:
command Sudow w !sudo tee % >/dev/null

Simon Permalink
March 09, 2014, 20:31

If "w!" fails, my usual approach is to ":shell", adjust the permissions, then exit back to vim where I can save it normally.

March 10, 2014, 09:28

This is a cool tip. Thanks.

Hilko Bengen Permalink
March 10, 2014, 12:51

There is no need to do "sudo vim /etc/apache/httpd.conf" -- use sudoedit (or sudo -e) instead. It launches your preferred VISUAL or EDITOR for editing a copy of the file.

Benjamin Johnson Permalink
March 11, 2014, 11:31

Tim Popes awesome Eunuch does this quite neatly along with some other useful things

Sven Putteneers Permalink
March 19, 2014, 15:44

I've cmapped it to w!!

Less to type and in my mind it translates to "REALLY write the fscking file" :)

July 01, 2014, 07:46

that is so special and try

July 04, 2014, 08:30

Maybe that will be something good

July 21, 2014, 10:33

Ok it is so great

August 07, 2014, 06:14

thanks for the share.
KBC 2014 grand premiere.

August 07, 2014, 06:15

Thanks for the share
KBC 2014 grand premiere.

September 12, 2014, 10:32

hanks for distributing fabulous data. It' s my pleasure to read it.I have also bookmarked you for ascertaining out new mails.

November 12, 2014, 08:00

Do you use Whatsapp. Check out Whatsapp dp, dp for Whatsapp
Best Whatsapp dp.

chirag Permalink
November 14, 2014, 08:17

Why can't we use sudoedit or sudo -e directly.This looks a better option.
Love status.

November 20, 2014, 17:00

thanks for sharing this site

good blog

November 20, 2014, 17:01

i love this blog

November 26, 2014, 18:06 always giving the most important and valuable information

Harish Permalink
November 27, 2014, 04:44

Even i am also get this error "/etc/apache/httpd.conf" E212: Can't open file for writing Press ENTER or type command to continue, how can i do it in windows machine?. and also find all basic bank details here.

November 27, 2014, 23:26

great blog, this blog is really informative and this is what im searching for

November 27, 2014, 23:27

nice, never seen a blog like this before. i just wanna share this to my friends

December 01, 2014, 00:31

Great blog , tihs is really informative

December 01, 2014, 00:33

maybe someday u can visit my blog too at
Prediksi Bola Akurat

Kaur deep Permalink
December 04, 2014, 03:08

Excellent and knowledgeable links and post.thanks for sharing.
Now, The Time has come for the Happy New Year 2015. It wil bring a lot fo happinesin our life.
Happy New Year 2015
And I am sure, You wil anjoy this new year day with yoru fiends and wil send some images wishes,
New Year 2015

Happy New Year 2015 Wishes
Greetings to yoru close ones, Your friends, You lover, etc..
Happy New Year 2015 Images
Even I am waititng for that day, because it just changes our life.
Happy New Year 2015 Greetings

Happy New Year 2015 SMS
So, That's why I am commenting here to wish you a very happy new year.
Happy New Year 2015 Messages
I hope this New year 2015 wil bring success to your website. But before that Merry Christmas is coming.
Merry Christmas Quotes
It is also a big event for everyone.
Merry Christmas Pictures
Everyone knows that Christmas is a day when People seems to be very happy, they just leave all sadness behind.
Merry Christmas Wallpapers
And I am sure, you also enjoy this Merry Christmas day of 2014.
Christmas day Images

Christmas stocking
But if you are not looking out to enjoy this christmas, then you are missing out on a big thing, because it really brings happiness.
Merry Christmas Images

Merry Christmas 2014
So, what you are waiting for?
Merry Christmas 2015
Enjoy This Merry Christmas day. Cheers :).

Merry Xmas Permalink
December 08, 2014, 14:24

charlie bit it really hard but that was not any factor in the way at all and we all decided to go.

Merry Christmas 2014

2014 Merry Christmas Greetings

2014 Merry Christmas

Merry Christmas Greetings In Russian

Russian Christmas Greetings

Merry Christmas Quotes 2014

Merry Christmas Messages 2014

Thank you brother.

Jaavarsingh Permalink
December 14, 2014, 18:49

Yeah its very simple i tried it on my machine today.. Very informative stuff. Thanks for sharing with us. keep posting similar post. and at the same time have a look at this site to find State Bank of India branch details.

December 17, 2014, 04:51

December 17, 2014, 04:51

Diana Permalink
December 17, 2014, 15:51

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

Beautiful Christmas Quotes for your friends and family...
christmas quotes
Christmas Messages for Whatsapp

Best Christmas Greetings for your friends and family...
christmas greeting
christmas wishes

What to Write in Christmas Card....... Check out best Christmas Greeting Card Words
What to Write in Christmas Card

Get Beautiful and Unique Christmas Wallpapers for free
free christmas wallpaper
christmas tree decorating ideas

Thanks for sharing such a great article.

December 18, 2014, 16:46

Great information.very interesting and informative blog. your article solve my many problems. i am very glad to read your blog.
latest missing you sms in english

friendship sms messages in hindi.

Anusha Holla Permalink
December 19, 2014, 04:21

Nice post on sudo vim, I use Mac so i think this might be helpful if i share with you. MacVim comes with mvim, a command-line wrapper that allows you to launch the MacVim GUI from the command-line with $ mvim filename and the MacVim CLI executable directly in your shell with $ mvim -v filename. Thanks for sharing post on sudo vim with us and at the same time Find here how Colud has changes Datacenter technology.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "lcd_417": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.