美文网首页
VIM 命令权限配置

VIM 命令权限配置

作者: 思考蛙 | 来源:发表于2019-05-05 22:04 被阅读0次

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:

相关文章

网友评论

      本文标题:VIM 命令权限配置

      本文链接:https://www.haomeiwen.com/subject/ldtivqtx.html