Vim 对所有文件类型运行 autocmd,除了
我有一个 Vim autocmd,可以在写入之前删除文件中的尾随空格。我几乎 100% 都希望这样做,但有一些文件类型我希望禁用它。传统的做法是在逗号分隔的列表中列出您希望 autocmd 运行的文件类型,例如:
autocmd BufWritePre *.rb, *.js, *.pl
但在这种情况下,这将是繁重的。
有没有办法将 autocmd 模式与除与该模式匹配的文件之外的所有文件进行匹配?我在文档中找不到与 NOT 匹配器等效的内容。
I have a Vim autocmd that removes trailing whitespace in files before write. I want this almost 100% of the time, but there are a few filetypes that I'd like it disabled. Conventional wisdom is to list the filetypes you want an autocmd to run against in a comma-separated list, eg:
autocmd BufWritePre *.rb, *.js, *.pl
But in this case that would be onerous.
Is there a way to match an autocmd pattern against all files EXCEPT those matching the pattern? I cannot find the equivalent to a NOT matcher in the docs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
*.rb
不是文件类型。这是一个文件模式。ruby
是文件类型,甚至可以在没有.rb
扩展名的文件上设置。因此,您最可能想要的是 autocmd 调用的函数,以检查不应执行操作的文件类型并删除空格。根据埃文的答案,您可以检查缓冲区局部变量并确定是否使用该变量进行剥离。如果您决定不想删除通常会删除的文件类型的缓冲区,这还允许您一次性禁用。
*.rb
isn't a filetype. It's a file pattern.ruby
is the filetype and could even be set on files that don't have a.rb
extension. So, what you most likely want is a function that your autocmd calls to both check for filetypes which shouldn't be acted on and strips the whitespace.Building on evan's answer, you could check for a buffer-local variable and determine whether to do the strip using that. This would also allow you to do one-off disabling if you decided that you don't want to strip a buffer that's a filetype you normally would strip.
单行方式的另一种选择:
然后您可以对除黑名单中的文件类型之外的所有文件类型执行您喜欢的操作。
Another choice of one line way:
Then you can do something you like for all filetypes except those in blacklist.
一个好方法是将一个文件类型的局部变量设置为 true。然后,如果该变量为 false(如果为其他所有内容设置)或者它根本存在(无需预设它),则设置自动命令。
根据注释更改变量前缀
A good way would be to set a local variable for the one filetype to true. Then set the automcommand if that variable is false (if set for everything else) or if it exists at all (no need to preset it).
changed variable prefixes based on comment
您可以在同一个正则表达式上执行 except 操作:
这将对除
.out
或.diffs
之外的所有文件扩展名执行
操作。You can do the except on the same regexp:
That will do
<your_command>
for all files extensions except for.out
or.diffs
.这适用于
语法
自动命令,其中模式(
)只是文件类型。它排除任何rst
文件:This works for
Syntax
autocommands, where the pattern (<match>
) is just the filetype. It excludes anyrst
files:我们的 .vimrc 配置文件在启动时仅运行一次。所以如果你此时进行 if 测试,它将不起作用,因为当前没有 python 文件正在编辑。
但是您可以使用 .vimrc 来设置自动行为:vim 每次遇到特殊条件时都会执行此操作。条件可以是您的情况:“正在编辑一个新文件,其文件类型是‘python’”。参见:h:au
Our .vimrc config file runs only once on startup. So if you put an if test at this time, it won't work, because no python file is then currently being edited.
But you can use .vimrc to set up an automatic behaviour: something that vim will do each time it encounters a special condition. The condition can be in your case: "A new file is being editing, and its file type is 'python'". See :h :au
这已经很晚了,但我认为您可以使用两个 autocmd,一个执行“默认”行为,另一个执行“例外”行为。一个例子是,当我想将除 HTML 之外的所有文件类型的选项卡大小设置为 4 时,我希望将其设置为 2。我可以这样配置:
This is very late, but I think you can use two autocmd, one does the 'default' behavior and the other does the 'except' behavior. One example is when I want to set tab size to four for all file types, except HTML, in which I want it to be two. I can configure it like this: