Vim 对所有文件类型运行 autocmd,除了

发布于 2024-11-17 12:46:16 字数 272 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

合久必婚 2024-11-24 12:46:16

*.rb 不是文件类型。这是一个文件模式。 ruby 是文件类型,甚至可以在没有 .rb 扩展名的文件上设置。因此,您最可能想要的是 autocmd 调用的函数,以检查不应执行操作的文件类型并删除空格。

fun! StripTrailingWhitespace()
    " Don't strip on these filetypes
    if &ft =~ 'ruby\|javascript\|perl'
        return
    endif
    %s/\s\+$//e
endfun

autocmd BufWritePre * call StripTrailingWhitespace()

根据埃文的答案,您可以检查缓冲区局部变量并确定是否使用该变量进行剥离。如果您决定不想删除通常会删除的文件类型的缓冲区,这还允许您一次性禁用。

fun! StripTrailingWhitespace()
    " Only strip if the b:noStripeWhitespace variable isn't set
    if exists('b:noStripWhitespace')
        return
    endif
    %s/\s\+$//e
endfun

autocmd BufWritePre * call StripTrailingWhitespace()
autocmd FileType ruby,javascript,perl let b:noStripWhitespace=1

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

fun! StripTrailingWhitespace()
    " Don't strip on these filetypes
    if &ft =~ 'ruby\|javascript\|perl'
        return
    endif
    %s/\s\+$//e
endfun

autocmd BufWritePre * call StripTrailingWhitespace()

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.

fun! StripTrailingWhitespace()
    " Only strip if the b:noStripeWhitespace variable isn't set
    if exists('b:noStripWhitespace')
        return
    endif
    %s/\s\+$//e
endfun

autocmd BufWritePre * call StripTrailingWhitespace()
autocmd FileType ruby,javascript,perl let b:noStripWhitespace=1
忆梦 2024-11-24 12:46:16

单行方式的另一种选择:

let blacklist = ['rb', 'js', 'pl']
autocmd BufWritePre * if index(blacklist, &ft) < 0 | do somthing you like | endif

然后您可以对除黑名单中的文件类型之外的所有文件类型执行您喜欢的操作。

Another choice of one line way:

let blacklist = ['rb', 'js', 'pl']
autocmd BufWritePre * if index(blacklist, &ft) < 0 | do somthing you like | endif

Then you can do something you like for all filetypes except those in blacklist.

嘿嘿嘿 2024-11-24 12:46:16

一个好方法是将一个文件类型的局部变量设置为 true。然后,如果该变量为 false(如果为其他所有内容设置)或者它根本存在(无需预设它),则设置自动命令。

autocmd BufWritePre *.foo let b:foo=true

if !exists("b:foo")
    autocmd ...
endif

根据注释更改变量前缀

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

autocmd BufWritePre *.foo let b:foo=true

if !exists("b:foo")
    autocmd ...
endif

changed variable prefixes based on comment

夏了南城 2024-11-24 12:46:16

您可以在同一个正则表达式上执行 except 操作:

autocmd BufWritePre *\(.out\|.diffs\)\@<! <your_command>

这将对除 .out.diffs 之外的所有文件扩展名执行 操作。

You can do the except on the same regexp:

autocmd BufWritePre *\(.out\|.diffs\)\@<! <your_command>

That will do <your_command> for all files extensions except for .out or .diffs.

黄昏下泛黄的笔记 2024-11-24 12:46:16

这适用于语法自动命令,其中模式()只是文件类型。它排除任何 rst 文件:

au Syntax *\(^rst\)\@<! …

This works for Syntax autocommands, where the pattern (<match>) is just the filetype. It excludes any rst files:

au Syntax *\(^rst\)\@<! …
强者自强 2024-11-24 12:46:16

我们的 .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

何以畏孤独 2024-11-24 12:46:16

这已经很晚了,但我认为您可以使用两个 autocmd,一个执行“默认”行为,另一个执行“例外”行为。一个例子是,当我想将除 HTML 之外的所有文件类型的选项卡大小设置为 4 时,我希望将其设置为 2。我可以这样配置:

# for all file types
autocmd FileType * se tabstop=4
# except HTML
autocmd FileType html se tabstop=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:

# for all file types
autocmd FileType * se tabstop=4
# except HTML
autocmd FileType html se tabstop=2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文