Linux 编辑文件并使用 :w 保存后,在 vim 中删除文件的“只读”属性!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3738623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:11:56  来源:igfitidea点击:

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

linuxunixvimreadonly

提问by Somebody still uses you MS-DOS

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

当我:w!在 vim 中保存文件时,我想更改文件中的只读属性。我该怎么做?(我不介意我是否必须调用外部脚本)。

I'm using Linux.

我正在使用 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 /tmp/* !sh /tmp/script.sh. 所以,我想在调用时:w!调用 chmod 命令:chmod 命令将是这样的:

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?

那么,我该怎么做 ":w!" 健康)状况?有可能还是我需要使用其他结构?

采纳答案by okay zed

The v:cmdbang is what you are looking for.

v:cmdbang 正是您要找的。

function! g:ChmodOnWrite()
  if v:cmdbang
    silent !chmod u+w %
  endif
endfunction

autocmd BufWrite * call g:ChmodOnWrite()

回答by zwol

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.

shell 命令chmod u+w <name of file>执行您想要的操作。我不知道如何从 vim 内部调用它。

回答by strager

You should be able to use just *:

您应该能够仅使用*

autocmd BufWrite * !chmod u+w %

It may be better to use BufWriteCmdinstead. I think that if the chmodfails, Vim will not attempt to write.

使用它可能会更好BufWriteCmd。我认为如果chmod失败,Vim 不会尝试写入。