Vim Tips Wiki
Edit Page
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 3: Line 3:
 
|previous=974
 
|previous=974
 
|next=976
 
|next=976
|created=2005
+
|created=August 18, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=Bernard Pratz
 
|author=Bernard Pratz
Line 11: Line 11:
 
|category2=
 
|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.
+
Sometimes you modify a file, for example from the <tt>/etc</tt> 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.
 
Of course, this tip has to be used with caution, as it gets you more power.
   
 
==Suggestion 1==
 
==Suggestion 1==
If you find you do not have permission to perform <code>:w</code>, use the following:
+
If you find you do not have permission to perform <tt>:w</tt>, use the following:
 
<pre>
 
<pre>
:w !sudo tee "%" > /dev/null
+
:w !sudo tee % > /dev/null
 
</pre>
 
</pre>
   
You can make a command so <code>:W</code> invokes sudo:
+
You can make a command so <tt>:W</tt> invokes sudo:
 
<pre>
 
<pre>
command W w !sudo tee "%" > /dev/null
+
command W w !sudo tee % > /dev/null
 
</pre>
 
</pre>
   
Line 50: Line 50:
   
 
==Suggestion 3==
 
==Suggestion 3==
Here is a mapping to save to a <code>/tmp</code> file, then overwrite the working file.
+
Here is a mapping to save to a <tt>/tmp</tt> 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>
 
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>
Line 57: Line 57:
 
'''Warning''' This command will reload the file; you will lose the modifications history (undo will not work, although it does keep a backup).
 
'''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 '<code>nobackup</code>' is set.
+
Note that a backup is made, even when '<tt>nobackup</tt>' is set.
   
 
==Comments==
 
==Comments==
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).
+
Use {{script|id=729}} which has had more testing.
   
 
----
 
----
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)