Remove the read-only attribute in vim after editing it and save with: w!

I want to change a read-only attribute from a file when I save it with :w! in vim. How can I do it? (I do not mind if I need to call an external script).

I am using Linux.

I know that with this command I can use an external script: autocmd BufWrite /tmp/* !sh /tmp/script.sh . So, I would like to call the chmod command when calling :w! : the chmod command would be something like this:

 autocmd BufWrite <:w! condition> !chmod u+w % 

So how do I do this: w! state? Is it possible for me to use a different structure?

+4
source share
3 answers

V: cmdbang is what you are looking for.

 function! g:ChmodOnWrite() if v:cmdbang silent !chmod u+w % endif endfunction autocmd BufWrite * call g:ChmodOnWrite() 
+4
source

You can use only * :

 autocmd BufWrite * !chmod u+w % 

Better to use BufWriteCmd . I think that if chmod does not work, Vim will not try to write.

+2
source

The chmod u+w <name of file> shell chmod u+w <name of file> does what you want. I do not know how to do this from inside vim.

0
source

All Articles