views:

95

answers:

3

I want to change the read only attribute from a file when I save it with :w! in vim. How do I do it? (I don't mind if I have to call an external script).

I'm using Linux.

I know I can use an external script using this command: autocmd BufWrite /tmp/* !sh /tmp/script.sh. So, I would like to call a chmod command when :w! is invoked: the chmod command is going to be something like this:

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

So, how do I do the ":w!" condition? Is it possible or do I need to use another structure?

A: 

The shell command chmod u+w <name of file> does what you want. I don't know offhand how to invoke that from inside vim.

Zack
I can use an external script using this command: autocmd BufWrite /tmp/* !sh /tmp/script.sh. I've edited my question with more information.
Somebody still uses you MS-DOS
+2  A: 

You should be able to use just *:

autocmd BufWrite * !chmod u+w %

It may be better to use BufWriteCmd instead. I think that if the chmod fails, Vim will not attempt to write.

strager
But isn't this command going to be called from every ":w" instead of just ":w!"?
Somebody still uses you MS-DOS
+2  A: 

The 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()
okay zed
This is a REALLY neat solution, I've checked the cmdbang documentation http://vimdoc.sourceforge.net/htmldoc/eval.html#v:cmdbang to understand it's behavior. I knew vim would have something like this! Well done!
Somebody still uses you MS-DOS