Change file permissions in Vim with open file

I vim somefile.txt often without using sudo. Is it possible to use sudo within an open vim session so you don't have to close the file and reopen? New to Vim and Linux in general so any help is very appreciated. Thank you.


我在.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. 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.


You can use the SudoEdit plugin; it provides a :SudoWrite command.

There's also a trick of :write to a process of !sudo tee , passing the current file name; it is explained here.

链接地址: http://www.djcxy.com/p/49586.html

上一篇: 当需要sudo权限时使用vim写入文件

下一篇: 使用打开的文件更改Vim中的文件权限