How do I make a file writable from inside Vim
I am frequently opening files that are readonly, I would li开发者_开发技巧ke to know if there is a way to make them writable from inside Vim. I do not mean :w!
, I need the file to be writeable after I close Vim.
If you are on a unix based machine you can always just use the unix command.
:!chmod 777 %
http://ss64.com/bash/chmod.html
Otherwise on windows, you should look into the calcs command: http://www.delawarepchelp.com/system/dos/calcs.htm
The issue comes from the fact that after changing the file attributes Vim will detect it and will try to reload the file and you will loose what you typed.
The best seems to create a function that redefines the autocmd FileChangedShell
to do nothing when the attribute change is detected.
See this example of setting file attributes without reloading a buffer where an example is given for making all files executables.
This should do the trick for you
I found this solution elsewhere:
:set readonly!
:set modifiable
my situation was that vim was opening my file, which I chmod 777 from the commandline before opening, as readonly. Not sure if this helps you at all...
edit: forgot the second command. only the combination of the two worked.
you can use :! to execute shell commands. So you could change the user using :!chown myusername filename
or change the access right using for example :!chmod o+w filename
Additionally, if the file was writeable only by another user (and you are not owner, so can't chmod it) you might map this command:
:command SuWrite %!sudo tee %
Of course, sudo -u username for other users
!sudo chmod 777 %
When it asks if you want to use the buffer, type "O" for ok.
You can use the SudoWrite
command from the vim-eunuch plugin, which enables you to write to a file with sudo even if you forgot to open vim with sudo. It's a great way to eliminate that annoying behavior when you forget.
精彩评论