vim 中 javascript 字符串中的 HTML 语法高亮显示
我不知道这是否可能/明智,但我很好奇是否可以让 javascript 文件中的字符串具有 html 突出显示。我发现php中的字符串可以有SQL语法高亮,所以我相信这是可能的。
但是,我不知道 vim 脚本,所以对此的任何帮助表示赞赏。
我正在使用 更好的 Javascript 语法 。
PS:如果有一个选项可以在编辑 js 文件时打开和关闭它,那就太好了,
谢谢
I don't know if this is possible/sensible, but I was curious to know if I can have my strings in javascript files have html highlighting. I discovered that strings in php could have SQL syntax highlighting, so I believe it is possible.
But, I don't know vim-scripting, so any help on this appreciated.
I am using the Better Javascript syntax.
PS: If there could be an option to turn it on and off while editing a js file, that would be wonderful
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您不介意一些语法文件黑客攻击,这是可能的。首先,您需要在 Javascript 语法文件中包含 HTML 语法文件 - 请参阅
:help syn-include
了解相关信息;其次,您需要声明 HTML 语法可以在某些元素(即字符串)内部找到。第三,如果您想要启用和禁用它的选项,您可以使这些命令依赖于全局变量,并编写一些设置或取消设置该变量的映射,然后重新加载语法文件。有关包含如何工作的示例,请查看
syntax/html.vim
(其中包括 Javascript 和 CSS 语法文件)、syntax/perl.vim
(其中包括POD 语法文件),或php.vim
(其中包括字符串中的 SQL 语法高亮显示,以全局变量为条件)。编辑:在我的副本中实际实现这一点做了一些工作。
在
syntax/javascript.vim
的头部,在syn caseignore
的正下方,添加然后将
@javaScriptHTML
添加到contained=< /code> 列出了
javaScriptStringD
和javaScriptStringS
。最后,您必须编辑
syntax/html.vim
以防止它尝试包含syntax/javascript.vim
(如果它是从 javascript 加载的):找到读取的行并将其更改为
Yes, it's possible if you don't mind some syntax file hacking. First you need to include the HTML syntax file from within the Javascript syntax file -- see
:help syn-include
for info on that; second you need to declare that HTML syntax can be found inside of certain elements (i.e. strings). Third, if you want to have the option of enabling and disabling it, you can make those commands dependent on a global variable, and write some mappings that set or unset the variable and then reload the syntax file.For examples on how inclusion works, take a look at
syntax/html.vim
(which includes the Javascript and CSS syntax files),syntax/perl.vim
(which includes the POD syntax file), orphp.vim
(which includes SQL syntax highlighting in strings, conditional on a global ariable).Edit: did some work on actually making this happen in my copy.
In the head of
syntax/javascript.vim
, just belowsyn case ignore
, addThen add
@javaScriptHTML
to thecontained=
lists forjavaScriptStringD
andjavaScriptStringS
.Finally you have to edit
syntax/html.vim
to prevent it from trying to includesyntax/javascript.vim
if it was loaded from javascript: find the line that readsand change it to