https://www.cyberciti.biz/faq/vim-vi-text-editor-save-file-without-root-permission/
What the vim and shell command does:
:w – Write a file (actually buffer).
!sudo – Call shell with sudo command.
tee – The output of write (vim :w) command redirected using tee.
% – The % is nothing but current file name. In this example, it is /etc/apache2/conf.d/mediawiki.conf. In other words tee command is run as root and it takes standard input (or the buffer) and write it to a file represented by %. However, this will prompt to reload file again (hit L to load changes in vim itself):
Update ~/.vimrc file
Open/Edit ~/.vimrc file and append the following code:
"
" Sample command W
"
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
Save and close the file. Open vim/vi and try to edit a privileged file with:
$ vi /etc/hosts
Now, write a privileged file with custom command just type W:
网友评论