经常作为普通用户权限在服务器上,改一个文件,弄完之后保存发现Permission Deny
。以前我都是傻乎乎的强行退出,然后在用root登录修改。其实一条命令就可以解决。
1 | :w !sudo tee % |
- :w - Write a file.
- !sudo - Call shell sudo command.
- tee - The output of write (vim :w) command redirected using tee. The % is nothing but current file name i.e. /etc/apache2/conf.d/mediawiki.conf. In other words tee command is run as root and it takes standard input and write it to a file represented by %.
只要按L
重载文件,就能看见之前的修改已经保存。比之前不保存退出再重进改方便很多,强烈推荐。
Reference: HowTo: Save A File In Vim / Vi Without Root Permission