vim 隐藏多个字符

发布于 2024-12-18 11:04:50 字数 298 浏览 4 评论 0原文

实际上我想在 haskell 文件中显示 -> (箭头后面有一个空格)。但我的印象是隐藏机制只能用一个字符替换 -> 。不良的效果是视觉上不良的压痕。

有办法实现这一点吗?

谢谢。

编辑:实际上我使用这个(来自 haskell.vim (隐藏增强)插件)

syntax match hsNiceOperator "<-" conceal cchar=←

Actually I'd like to display -> with (there is a space after the arrow) in haskell files. But I have the impression the conceal mechanism only work to replace -> by one character. An undesirable effect is visually bad indentation.

Is there a way to achieve this?

Thanks.

Edit: Actually I use this, (from haskell.vim (conceal enhancement) plugin)

syntax match hsNiceOperator "<-" conceal cchar=←

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

慕巷 2024-12-25 11:04:50

我在 C 中所做的正是您想要的。诀窍是单独隐藏每个字符,如下所示:

syn match ArrowHead contained ">" conceal cchar=▶
syn match ArrowTail contained "-" conceal cchar=─
syn match ArrowFull "->" contains=ArrowHead,ArrowTail

不幸的是,您可能会发现 ArrowHead 或 ArrowTail 在 ArrowFull 之外匹配。这是因为现有语法规则使用 contains=ALLBUT,... 或类似的内容。

为了在 C 中解决这个问题,我将 ArrowTail 和 ArrowHead 添加到 cParenGroup 集群中,这似乎可以防止出现任何问题。

syn cluster cParenGroup add=ArrowTail,ArrowHead

您可能需要为 Haskell 做类似的事情。

因为我根本不使用隐藏功能,所以我告诉 Vim 继续并始终“隐藏”箭头:

set conceallevel=1 concealcursor=nvic

顺便说一句,如果您不喜欢隐藏字符的默认颜色,您可以像这样更改它们:

hi conceal ctermfg=DarkBlue ctermbg=none guifg=DarkBlue guibg=none

I do exactly what you want in C. The trick is to conceal each character separately, like so:

syn match ArrowHead contained ">" conceal cchar=▶
syn match ArrowTail contained "-" conceal cchar=─
syn match ArrowFull "->" contains=ArrowHead,ArrowTail

You might find that ArrowHead or ArrowTail gets matched outside an ArrowFull, unfortunately. This is because existing syntax rules use contains=ALLBUT,... or something similar.

To fix this in C, I added ArrowTail and ArrowHead to the cParenGroup cluster, which seems to prevent any problems.

syn cluster cParenGroup add=ArrowTail,ArrowHead

You may need to do something similar for Haskell.

Since I don't use the conceal feature at all otherwise, I tell Vim to go ahead and "conceal" the arrows ALL the time:

set conceallevel=1 concealcursor=nvic

BTW if you don't like the default colors for the conceal chars, you can change them like this:

hi conceal ctermfg=DarkBlue ctermbg=none guifg=DarkBlue guibg=none
时光礼记 2024-12-25 11:04:50

超级老的问题,但如果其他人试图解决这个问题,我已经成功地完成了以下操作:

call matchadd('Conceal', '<-\&<', 10, -1, {'conceal':'←'})
call matchadd('Conceal', '<\zs-', 10, -1, {'conceal':' '})
hi Conceal        ctermbg=NONE ctermfg=red guifg=red

matchadd 的第二个参数是 。查看:help pattern告诉我们,模式是一个或多个由\|分隔的分支,并且一个branch 是一个或多个由 \& 分隔的 concat。从文档中:“[a branch] 匹配最后一个 concat,但前提是所有前面的 concat 也在同一位置匹配。”

因此,在第一次调用中,我们指定一个具有一个分支的模式(没有 \| 值),由两个 concat 组成。第一个 concat 匹配完整箭头,第二个匹配箭头的第一个字符。因此,最后一个 concat<,它是所有被视为隐藏参数匹配的内容,但这仅在 < 时匹配。 code> 出现在 <- 中。第二次调用给出了一个带有一个 branch 和一个 concatpattern。该模式以 < 开头,但使用 zero-width 原子 \zs 重置匹配,该原子会重置下一个字符的匹配, -,用空格隐藏。 hi Conceal 只是突出显示。

在我的测试中, < 本身不会变成 ,而 - 本身不会变成空格。

希望这对某人有帮助:)

Super-old question, but in case anyone else is trying to figure this out, I have had success with the following:

call matchadd('Conceal', '<-\&<', 10, -1, {'conceal':'←'})
call matchadd('Conceal', '<\zs-', 10, -1, {'conceal':' '})
hi Conceal        ctermbg=NONE ctermfg=red guifg=red

The second argument of matchadd is <pattern>. Looking at :help pattern tells us that a pattern is one or more branches separated by \|, and a branch is one or more concats separated by \&. From the docs: "[a branch] matches the last concat, but only if all the preceding concats also match at the same position."

So in the first call we specify a pattern with one branch (there are no \| values), made of two concats. The first concat matches the full arrow, and the second matches the first character of the arrow. Thus, the last concat is the < which is all that is taken as the match for the conceal parameter, but this only matches if the < appears in a <-. The second call gives a pattern with one branch and one concat. The pattern begins with the < but the match is reset with the zero-width atom \zs which resets the match at the next character, the -, which is concealed with a space. The hi Conceal is just highlighting.

In my testing < by itself doesn't get turned into and - by itself isn't turned into a space.

Hope this helps someone :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文