User Tools

Site Tools


vim:saving_a_file_as_root

This is an old revision of the document!


VIM - Saving a file as root

Forgot to edit a file as sudo user?

Want to overwrite a file without quitting.

:w !sudo tee %
</vi>
 
  * **:w** - write a file.
  * **!sudo** - Call shell with sudo command,
  * **tee** - The output of write command redirected using tee.
  * **%** - The % is nothing but current filename.
 
tee command is run as root and it takes standard input (or the buffer) and writes it to a file represented by %.
 
This will prompt to reload the file again - hit **L** to load changes within vi itself.
 
----
 
===== Update ~/.vimrc file =====
 
Append to ~./.vimrc file:
 
<file bash ~./vimrc>
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
</file>
 
Save and close the file.
 
Test, by opening a priviledged file without using sudo.  Example:
 
<code bash>
vi /etc/hosts

Edit the file, and write as a privileged file with custom command, just type:

<code vi> w: </vi>

sudo will require that you authenticate yourself, so enter your password when prompted.

vim/saving_a_file_as_root.1578534703.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki