vim 保存在Vim中编辑的只读文件
示例
有时,我们可能会打开一个文件,如果没有使用,我们将没有权限在Vim中进行写入sudo。
使用此命令保存在Vim中编辑的只读文件。
:w !sudo tee > /dev/null %
您可以:w!!在其中映射到.vimrc:
cmap w!! w !sudo tee > /dev/null %
如图所示,系统将提示您。
。
按O,文件将被保存。它在vi/vim中保持打开状态,以便进行更多编辑或读取,并且:q!由于文件仍以只读方式打开,因此可以通过键入正常退出。
命令说明
:w ............................ isn't modifying your file in this case,
............................ but sends the current buffer contents to
............................ a substituted shell command
!sudo ...................... call the shell 'sudo' command
tee .................. the output of the vi/vim write command is redirected
using the 'tee' command
> /dev/null ...... throws away the standard output, since we don't need
to pass it to other commands
% .... expands to the path of the current file资料来源:
亚当·库尔普(AdamCulp)的技术博客
Stackoverflow,vim“用sudo编写”技巧如何工作