有没有办法用正则表达式触发器制作出色的片段?

Is there a way of making sublime snippets with a regex trigger?

我正在尝试制作一个由正则表达式触发的片段。在 Sublime Text 3 中甚至可能吗?

我已经试过了,但没有触发。我已经检查过 sublime 是否使用“查找和替换”选项正确替换了它。

<snippet>
<content><![CDATA[_
]]></content>
<tabTrigger>([a-zA-z])(\d)</tabTrigger>
<description></description>
<scope>text.tex.latex</scope>
</snippet>

I want my snippet to be triggered by pressing tab after any word that matches a character followed by a digit and replace it with the character, a _, and the digit.

例子

a1 变成 a_1

X0 变为 X_0

我的猜测是,也许这个表达式,

(?i)(?<=<tabTrigger>[a-z])(?=\d<\/tabTrigger>)
例如,

替换为 _ 可能是您的想法。


If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


sublime-snippet 文件的触发文本必须是文字文本才能触发; Sublime 不会根据正则表达式匹配它。要执行类似的操作,您需要一个绑定到 tab 键(例如)的插件命令,该命令会触发一个检查光标左侧文本以查看其是否匹配的命令正则表达式,然后基于它展开它。

我不知道有这样的通用软件包(尽管 Emmet does this for expanding HTML tags, it's not generic and is known to interfere with regular tab completion) but there may be one listed on package control.

Sublime 论坛上的

This forum post 包含一个示例插件,它做的事情与此非常相似,但作为此类事情的起点可能很有用。根据您上面的示例,只要您将示例中的 scope 交换为代码段中的示例,它就应该执行您想要的操作,以便它在 LaTeX 文件而不是 Markdown 中触发。在这种情况下,您可能还想重命名命令。


[编辑] 如果您不确定如何在 Sublime Text 中使用插件,this video 介绍了如何使用。