FANDOM


 
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
{{Tip
+
{{TipImported
 
|id=975
 
|id=975
|title=su-write
+
|previous=974
|created=August 18, 2005 17:03
+
|next=976
  +
|created=August 18, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=Bernard Pratz
 
|author=Bernard Pratz
 
|version=6.0
 
|version=6.0
 
|rating=7/3
 
|rating=7/3
|text=
 
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...
 
 
 
 
TODO:
 
 
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("%")
 
 
}
 
 
 
 
}}
 
}}
  +
I just wrote a little function that uses sudo and cp to save a file whose modes wouldn't allow me to write it.
   
== Comments ==
+
Obviously, it preserves the modes of the original file, though it is being rewriten. Of course, this tip has to be used with real caution, as it gets you more power.
just use :
 
system('stat -c%a '.expand("%"))
 
instead of
 
system('find . -maxdepth 1 -name '.expand("%").' -printf "%m"')
 
   
useless use of find O:-)
+
TODO:
thanks to strull for the clever suggestion
+
*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.
   
--
+
<pre>
Guyzmo
+
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("%")
  +
endfunction
  +
</pre>
   
bernardATpratzDOTnet
+
==Comments==
, August 18, 2005 17:20
+
Just use:
----
 
I just forgot a detail, to restore the user/group of the original file :
 
   
function Suedit()
+
<pre>
let fname=tempname()
+
system('stat -c%a '.expand("%"))
exe 'w '.fname
+
instead of
let owner=system('stat -c%U:%G '.expand("%"))
+
system('find . -maxdepth 1 -name '.expand("%").' -printf "%m"')
let modes=system('stat -c%a '.expand("%"))
+
</pre>
exec '!sudo cp '.fname.' '.expand("%")
 
exec '!sudo chmod '.modes." ".expand("%")
 
exec '!sudo chown '.owner'" ".expand("%")
 
endfunction
 
   
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
+
Use {{script|id=729}} which has had more testing.
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 &#35;729] already does this.
+
It's very simple:
   
pagaltzis--AT--gmx.de
+
<pre>
, August 18, 2005 20:46
+
:w !sudo tee %
----
+
</pre>
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.
+
Minor improvement to prevent tee's stdout from "cluttering" your vim session:
 
pagaltzis--AT--gmx.de
 
, 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?
 
 
pagaltzis--AT--gmx.de
 
, August 20, 2005 3:47
 
----
 
:w !sudo tee %
 
 
It's very simple.
 
 
gorou
 
, August 23, 2005 22:40
 
----
 
&gt; :w !sudo tee %
 
&gt; It's very simple.
 
 
... and quite clever! ;-)
 
 
Minor improvement to prevent tee's stdout from "cluttering" your vim session:
 
 
:w !sudo tee % &gt; /dev/null
 
 
But Jake makes a good point, though: why not just:
 
 
sudo vi &lt;file_whose_modes_wouldn't_allow_me_to_write_it&gt;
 
 
Or, if you don't want to run your entire vim sesion as sudo:
 
 
[/scripts/script.php?script_id=729 vimscript &#35;729] : sudo.vim
 
 
Peter Vandenberk
 
 
 
pvdbp--AT--yahoo.co.uk
 
, August 24, 2005 3:51
 
----
 
:w !sudo tee % &gt; /dev/null
 
 
Is BRILLIANT!
 
 
[quote]
 
But Jake makes a good point, though: why not just:
 
 
sudo vi &lt;file_whose_modes_wouldn't_allow_me_to_write_it&gt;
 
   
Peter Vandenberk
+
<pre>
[/quote]
+
:w !sudo tee % &gt; /dev/null
  +
</pre>
   
Peter,
+
But why not just:
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
+
<pre>
  +
sudo vi &lt;file_whose_modes_wouldn't_allow_me_to_write_it&gt;
  +
</pre>
   
RichardBronosky (first--AT--last.com)
 
, July 12, 2006 8:02
 
 
----
 
----
<!-- parsed by vimtips.py in 0.686125 seconds-->
 

Revision as of 10:09, December 9, 2007

Tip 975 Printable Monobook Previous Next

created August 18, 2005 · complexity basic · author Bernard Pratz · version 6.0


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 rewriten. Of course, this tip has to be used with real caution, as it gets you more power.

TODO:

  • 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 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("%")
endfunction

Comments

Just use:

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

Use script#729 which has had more testing.


It's very simple:

:w !sudo tee %

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

:w !sudo tee % > /dev/null

But why not just:

sudo vi <file_whose_modes_wouldn't_allow_me_to_write_it>

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