Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=975
 
|id=975
 
|previous=974
 
|previous=974
 
|next=976
 
|next=976
|created=August 18, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Bernard Pratz
 
|author=Bernard Pratz
 
|version=6.0
 
|version=6.0
 
|rating=7/3
 
|rating=7/3
  +
|category1=
  +
|category2=
 
}}
 
}}
  +
Sometimes you modify a file, for example from the <code>/etc</code> directory, then when you try to save the file, you get a permission-denied error. This tip presents some suggestions for using sudo from with Vim, so you can successfullly write the file with temporary privileges.
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.
+
Of course, this tip has to be used with caution, as it gets you more power.
   
  +
==Suggestion 1==
TODO:
 
  +
If you find you do not have permission to perform <code>:w</code>, use the following:
*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.
 
 
<pre>
*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.
 
 
:w !sudo tee % > /dev/null
 
</pre>
   
  +
You can make a command so <code>:W</code> invokes sudo:
 
<pre>
  +
command W w !sudo tee % > /dev/null
 
</pre>
  +
  +
Or, if you know about the problem beforehand:
 
<pre>
  +
sudoedit path_to_file
  +
sudo -e path_to_file
 
</pre>
  +
  +
==Suggestion 2==
  +
The following function saves the current file to a temporary file, then copies the new file to replace the original. It preserves the modes of the original file, though it is being rewriten.
 
<pre>
 
<pre>
 
function Suedit()
 
function Suedit()
Line 30: Line 46:
 
</pre>
 
</pre>
   
 
'''Warning''' There is no check for symlinks. A symlink would be removed and replaced by a file with the symlink's modes, which are 777.
==Comments==
 
Just use:
 
   
  +
==Suggestion 3==
  +
Here is a mapping to save to a <code>/tmp</code> file, then overwrite the working file.
 
<pre>
 
<pre>
  +
nnoremap <leader>es :w! /tmp/sudoSave \| let $fileToSave=expand('%') \| let $fileToSaveBackup=expand('%').'~' \| !sudo cp $fileToSave $fileToSaveBackup && sudo cp /tmp/sudoSave $fileToSave<CR><ESC>:e!<CR>
system('stat -c%a '.expand("%"))
 
instead of
 
system('find . -maxdepth 1 -name '.expand("%").' -printf "%m"')
 
 
</pre>
 
</pre>
   
  +
'''Warning''' This command will reload the file; you will lose the modifications history (undo will not work, although it does keep a backup).
----
 
Use {{script|id=729}} which has had more testing.
 
   
  +
Note that a backup is made, even when '<code>nobackup</code>' is set.
----
 
It's very simple:
 
   
 
==Comments==
<pre>
 
  +
Use {{script|id=729}} which has had more testing or use {{script|id=2709}} which is an improved version of the first plugin (since it is not developed any more).
:w !sudo tee %
 
</pre>
 
 
----
 
Minor improvement to prevent tee's stdout from "cluttering" your vim session:
 
 
<pre>
 
:w !sudo tee % &gt; /dev/null
 
</pre>
 
 
But why not just:
 
 
<pre>
 
sudo vi &lt;file_whose_modes_wouldn't_allow_me_to_write_it&gt;
 
</pre>
 
   
 
----
 
----

Revision as of 06:00, 13 July 2012

Tip 975 Printable Monobook Previous Next

created 2005 · complexity basic · author Bernard Pratz · version 6.0


Sometimes you modify a file, for example from the /etc directory, then when you try to save the file, you get a permission-denied error. This tip presents some suggestions for using sudo from with Vim, so you can successfullly write the file with temporary privileges.

Of course, this tip has to be used with caution, as it gets you more power.

Suggestion 1

If you find you do not have permission to perform :w, use the following:

:w !sudo tee % > /dev/null

You can make a command so :W invokes sudo:

command W w !sudo tee % > /dev/null

Or, if you know about the problem beforehand:

sudoedit path_to_file
sudo -e path_to_file

Suggestion 2

The following function saves the current file to a temporary file, then copies the new file to replace the original. It preserves the modes of the original file, though it is being rewriten.

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

Warning There is no check for symlinks. A symlink would be removed and replaced by a file with the symlink's modes, which are 777.

Suggestion 3

Here is a mapping to save to a /tmp file, then overwrite the working file.

nnoremap <leader>es :w! /tmp/sudoSave \| let $fileToSave=expand('%') \| let $fileToSaveBackup=expand('%').'~' \| !sudo cp $fileToSave $fileToSaveBackup && sudo cp /tmp/sudoSave $fileToSave<CR><ESC>:e!<CR>

Warning This command will reload the file; you will lose the modifications history (undo will not work, although it does keep a backup).

Note that a backup is made, even when 'nobackup' is set.

Comments

Use script#729 which has had more testing or use script#2709 which is an improved version of the first plugin (since it is not developed any more).