Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing read-only file on GVim

Tags:

vim

vi

macvim

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.

like image 257
Blaszard Avatar asked Mar 19 '26 20:03

Blaszard


2 Answers

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.

like image 78
romainl Avatar answered Mar 22 '26 09:03

romainl


Either change the permissions/owner:

:!chown myuser %
:!chmod +w %

Or write using sudo over a pipe:

:w !sudo tee %
like image 40
sehe Avatar answered Mar 22 '26 10:03

sehe