3

Is there any way to edit read-only file on GVim?

If you use vim on console, sudo vim /path/to/file enables you to edit the read-only file. How can I edit it on already-opened MacVim window?

If you open :tabnew /path/to/file then edit it, and try to save it, then the error occurs saying xx is read-only (add ! to override). However, when you try to save it by :w!, the error still occurs saying xx Can't open file for reading.

I know, if you first change the permission of the file and edit it, and then reverts its permission, then you can edit it successfully... But I don't want to bother doing such a tedious thing...

Thanks.

4

3 回答 3

4

MacVim 带有mvim一个命令行包装器,它允许您从命令行启动 MacVim GUI,$ mvim filename并在您的 shell 中直接启动 MacVim CLI 可执行文件$ mvim -v filename

两者都可以很好地工作,sudo因此您可以完美地在新的 MacVim 窗口中以写入权限$ sudo mvim filename打开,或者在当前的 MacVim 窗口中执行相同的操作。filename$ sudo mvim --remote filename

于 2013-09-18T08:02:55.090 回答
3

更改权限/所有者:

:!chown myuser %
:!chmod +w %

sudo或使用管道编写:

:w !sudo tee %
于 2013-09-17T23:36:51.323 回答
1

如果你发现这个技巧tee太难记/太难输入,还有SudoEdit 插件,它定义了一个方便的:SudoWrite命令。

另见 Vim Tips Wiki 上的Su-write文章。

于 2013-09-18T06:53:56.153 回答