vim:saving_a_file_as_root
VIM - Saving a file as root
Forgot to edit a file as sudo user?
Want to overwrite a file without quitting.
:w !sudo tee %
- :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:
- ~./vimrc
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
Save and close the file.
Test, by opening a priviledged file without using sudo. Example:
vi /etc/hosts
Edit the file, and write as a privileged file with custom command, just type:
w:
sudo will require that you authenticate yourself, so enter your password when prompted.
vim/saving_a_file_as_root.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1