开发者

Remove a file's "read only" attribute in vim after editing it and saving with :w!

开发者 https://www.devze.com 2023-01-16 10:05 出处:网络
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 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 l开发者_如何学Goike 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?


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()


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消