Previous TipNext Tip

Tip: #975 - Su-write

Created: August 18, 2005 17:03 Complexity: basic Author: Bernard Pratz Version: 6.0 Karma: 7/3 Imported from: Tip#975

I just wrote a little function that uses sudo and cp to save a file whose modes wouldn't allow me to write it.

Obviously, it preserves the modes of the original file, though it is being rewrited. Of course, this tip has to

be used with real caution, as it gets you more power...


The only real drawback is there's no check for symlinks, and then, the symlink would get removed and

replaced by a file with the symlink's modes, which are 777...

Another thing that would be nice, would be to integrate it to the interface, to get it work when :w is not

enough, or at least having an alias like :wforce, :w!! or :whatever...

function Suedit() {

let fname=tempname() 
exe 'w '.fname 
let modes=system('find . -maxdepth 1 -name '.expand("%").' -printf "%m"') 
exec '!sudo cp '.fname.' '.expand("%") 
exec '!sudo chmod '.modes." ".expand("%") 



just use : system('stat -c%a '.expand("%")) instead of system('find . -maxdepth 1 -name '.expand("%").' -printf "%m"')

useless use of find O:-) thanks to strull for the clever suggestion

-- Guyzmo

bernardATpratzDOTnet , August 18, 2005 17:20

I just forgot a detail, to restore the user/group of the original file :

function Suedit()

let fname=tempname() 
exe 'w '.fname 
let owner=system('stat -c%U:%G '.expand("%")) 
let modes=system('stat -c%a '.expand("%")) 
exec '!sudo cp '.fname.' '.expand("%") 
exec '!sudo chmod '.modes." ".expand("%") 
exec '!sudo chown '.owner'" ".expand("%") 


this time, I hope it'll work correctly

-- Guyzmo (going to bed)

bernardATpratzDOTnet , August 18, 2005 17:26

I have made no tests on this script, that should work as is... When I'll have time, I'll make a script around that to check for all errors, with confirmation and all that stuff, and be free to do it before me, if you got the time to do so :P

-- Guyzmo

bernardATpratzDOTnet , August 18, 2005 17:36

Spare yourself the effort: [/scripts/script.php?script_id=729 vimscript #729] already does this. , August 18, 2005 20:46

Except when the system lacks sudo. (forex: vanilla FreeBSD and OpenBSD installs)

foltiATbalabitDOThu , August 19, 2005 3:58

What's your point? Of course you can't use permissions granted via sudo if you don't have sudo. , August 19, 2005 5:40

Why is sudo dragged into vim, use the shell.

Jake , August 19, 2005 15:15

For the same reason that wget or scp or rsync have been dragged into Vim? , August 20, 2005 3:47

w !sudo tee %

It's very simple.

gorou , August 23, 2005 22:40

> :w !sudo tee % > It's very simple.

... and quite clever! ;-)

Minor improvement to prevent tee's stdout from "cluttering" your vim session:

:w !sudo tee % > /dev/null 

But Jake makes a good point, though: why not just:

sudo vi <file_whose_modes_wouldn't_allow_me_to_write_it> 

Or, if you don't want to run your entire vim sesion as sudo:

[/scripts/script.php?script_id=729 vimscript #729] : sudo.vim

Peter Vandenberk , August 24, 2005 3:51

w !sudo tee % > /dev/null


[quote] But Jake makes a good point, though: why not just:

sudo vi <file_whose_modes_wouldn't_allow_me_to_write_it> 

Peter Vandenberk [/quote]

Peter, I often forget to open a file as sudo, or open a file expecting to only view it, and then find a problem with it. This saves me from having to:

w /tmp/%

Then sudo mv the file in place after I exit. I love it!

I also try to learn to do things without plugin and custom mappings so that I can use it on any system. I use hundreds of servers and rarely have access to my .vimrc

RichardBronosky ( , July 12, 2006 8:02

Community content is available under CC-BY-SA unless otherwise noted.