用 vim
打开没有写权限的文件而忘记用 sudo
时,文件变成 read-only
:w!
, SHIFT+ZZ
,或者 qw!
都无法保存 :w !sudo tee %
:q!
退出 - !sudo : Call shell sudo command.
- tee : The output of the vi/vim write command is redirected using tee.
- % : Triggers the use of the current filename.
Simply put, the ‘tee’ command is run as sudo and follows the vi/vim command on the current filename given.
ref: