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:

:wq!

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:

:q!

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:

:sudow

See you!

Comments

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

Exactly!

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!

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.

Chankey Pathak Permalink
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 https://github.com/tpope/vim-eunuch

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" :)

Stephan George Permalink
January 18, 2015, 02:42

Looks simple, but i want to save .htaccess file in web root directory its not taking up, please can anyone help me in this...? And you can check here to know more how cloud has changed the 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 "0day_417": (just to make sure you're a human)

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

Advertisements