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.

有帮助吗?

解决方案

MacVim comes with mvim, a command-line wrapper that allows you to launch the MacVim GUI from the command-line with $ mvim filename and the MacVim CLI executable directly in your shell with $ mvim -v filename.

Both work well with sudo so you can perfectly do $ sudo mvim filename to open filename with write privileges in a new MacVim Window or $ sudo mvim --remote filename to do the same in the current MacVim window.

其他提示

Either change the permissions/owner:

:!chown myuser %
:!chmod +w %

Or write using sudo over a pipe:

:w !sudo tee %

If you find the trick with tee too hard to remember / too difficult to type, there's also the SudoEdit plugin, which defines a convenient :SudoWrite command.

Also see the Su-write article on the Vim Tips Wiki.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top