2013-02-08 90 views
0

如果您在vi或vim中打開文件並且沒有權限寫入,是否有辦法以sudo su而不必丟棄所有更改並重新打開?Vi escelate用戶權限

目前,我必須放棄所有更改,關閉vi然後su vi

+3

看看這個帖子:http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work。當你不記得那裏提供的魔法咒語時,你總是可以:w /tmp/somefilename.txt,所以至少你不會丟失內容。 – 2013-02-08 20:06:04

+0

嗯,我的意思是,如上所示,有解決方法,但你至少應該將你的修改寫入'/ tmp/blah',然後'sudo cp/tmp/blah dest-file'而不是放棄所有您每次更改。 – meagar 2013-02-08 20:07:30

+0

雖然你可能會在這裏得到一些答案,但投票把它移到unix.stackexchange.com是合適的。編輯:將其移到unix.stackexchange.com不是標誌列表中的一個選項。主持人可能會採取行動嗎? – 2013-02-08 20:17:01

回答

1

如果你安裝了the Sudo Edit plugin,你會得到命令:SudoWrite,它會詢問你的root密碼。然後它將以root身份寫入文件,非常方便。我經常用它來編輯/ etc /中的東西。

從文檔:

2.2 SudoWrite       *SudoWrite* 

:[range]SudoWrite[!] [file] 

SudoWrite will write the given file using any of the configured methods for 
superuser authtication. It basically does something like this: > 

    :w !sudo tee >/dev/null file 

If no filename is given, SudoWrite will try to write the current file name. 
If the current buffer does not contain any file, it will abort. 

You can specify a range to write just like |:w|. If no range is given, it will 
write the whole file. If the bang argument is not given, the buffer will only 
be written, if it was modified. 

Again, you can use the protocol handler sudo: for writing.