Save a file you edited in vim without the needed permissions (no echo)原文地址:http://www.commandlinefu.com/commands/view/5147/save-a-file-you-edited-in-vim-without-the-needed-permissions-no-echoSave a file you edited in vim without the needed permissions (no echo)
Write a file you edited in Vim but that you do not have the permissions to write to (unless you use sudo.) Same as #1204 but without the echo to stdout that I find annoying.
不用sudo等來儲存一個需要編輯許可權的檔案,儲存一個你在寫的檔案檔案,你發現你沒有許可權去寫(除非你用sudo),此時使用下面的命令就可以解決
下面的命令解釋,寫檔案到一個命令,tee讀取stdin, 寫到stdout和檔案中,由於使用了sudo,所以可以寫迴文件,個人測試不用>/dev/null也可以直接就stdout到檔案了:w !sudo tee > /dev/null %文章評論部分::w !sudo tee %Save
a file you edited in vim without the needed permissions
I often forget to sudo before editing a file I don't have write permissions on. When you come to save that file and get the infamous "E212: Can't open file for writing", just issue that vim command in order to save the file without the need to save it to a
temp file and then copy it back again.
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
Save a file you edited in vim without the needed permissions
Calls sudo tee like all the other lines, but also automatically reloads the file.
Optionally you can add
command Wq :execute ':W' | :q
and
command WQ :Wq
to make quitting easier