Blaszard
Blaszard

Reputation: 31965

Editing read-only file on GVim

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.

Upvotes: 3

Views: 6320

Answers (3)

romainl
romainl

Reputation: 196556

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.

Upvotes: 4

Ingo Karkat
Ingo Karkat

Reputation: 172580

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.

Upvotes: 1

sehe
sehe

Reputation: 393064

Either change the permissions/owner:

:!chown myuser %
:!chmod +w %

Or write using sudo over a pipe:

:w !sudo tee %

Upvotes: 3

Related Questions