用 vim
打開沒有寫權限的文件而忘記用 sudo
時,文件變成 read-only
這時候用 :w!
, SHIFT+ZZ
,或者 qw!
都無法保存
:w !sudo tee %
:w : Write a file.可以將文件寫入,文件仍然是只讀模式,通過 :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.