简体   繁体   English

使用打开的文件更改Vim中的文件权限

[英]Change file permissions in Vim with open file

I vim somefile.txt often without using sudo. 我经常在不使用sudo的情况下访问vim somefile.txt Is it possible to use sudo within an open vim session so you don't have to close the file and reopen? 是否可以在打开的vim会话中使用sudo,这样您就不必关闭文件并重新打开? New to Vim and Linux in general so any help is very appreciated. 一般都是Vim和Linux的新手,所以非常感谢任何帮助。 Thank you. 谢谢。

I have this in my .vimrc, so that a :W will write with sudo access. 我在我的.vimrc中有这个,所以a :W会用sudo访问写。

" :W sudo saves the file                                                                                  
command W w !sudo tee % > /dev/null`

Well seems like I was late to the party. 好像我迟到了。 As said you should use :w !sudo tee % to switch to sudo. 如上所述你应该使用:w !sudo tee %切换到sudo。 If you would like to change the permissions of the file so you will not need sudo again you could use :!chmod +w % in command mode with % getting replaced by the filename to change the permissions of the file afterwards, as stated here: How to change file permission from within vi . 如果你想更改文件的权限,所以你不再需要sudo,你可以使用:!chmod +w %在命令模式下, % gets会被文件名替换,以便之后更改文件的权限,如下所述: 如何从vi中更改文件权限

You can use the SudoEdit plugin ; 你可以使用SudoEdit插件 ; it provides a :SudoWrite command. 它提供了一个:SudoWrite命令。

There's also a trick of :write to a process of !sudo tee , passing the current file name; 还有一个技巧:write !sudo tee的进程,传递当前文件名; it is explained here . 这里解释一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM