使用vim編輯檔案時遇到沒有許可權的解決辦法

來源:互聯網
上載者:User
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.