vim儲存root許可權的檔案 在Linux,沒有sudo 就直接用vim 編輯/etc/內的檔案,等編輯好了之後,使用vim儲存時,得到提示說檔案無法儲存,這時候才發現沒許可權。針對這種問題,目前有如下幾種解決方案。1. vi /etc/httpd.conf 儲存時,用命令:w !sudo tee % :w - Write a file. !sudo - Call shell sudo command. tee - The output of write (vim :w) command is redirected using tee. The % is nothing but current file name i.e. /etc/httpd.conf. In other words tee command is run as root and it takes standard input and write it to a file represented by %. However, this will prompt to reload file again (hit L to load changes in vim itself).強烈推薦這一種用法。不過,首先得保證運行vim的使用者有sudo的許可權。 2.編輯使用者$HOME/.vimrc檔案將第一種方案的比較難記的命令重新命名一下,下次可以直接使用。 vim $HOME/.vimrc 添加如下的一句話,並儲存。 command -nargs=? Sudow :w !sudo tee % 今後用vim編輯時,需要sudo儲存時,直接用此處定義的Sudow命令儲存即可。 3. 先儲存到一個臨時檔案中,然後用root去拷貝它去覆蓋需要編輯的檔案。 可能用user帳號開啟的一個檔案,vim /etc/httpd.conf,然後在vim編輯號後,用:w /tmp/httpd.conf即可儲存為一個臨時檔案。 4. 使用vim之時直接用sudo vim也可以,哈哈,如 sudo vim /etc/httpd.conf