简体   繁体   English

在vim中使用Javascript语法突出显示

[英]Javascript syntax highlighting in vim

Has anyone else found VIM's syntax highlighting of Javascript sub-optimal? 有没有其他人发现VIM的语法突出显示Javascript次优? I'm finding that sometimes I need to scroll around in order to get the syntax highlighting adjusted, as sometimes it mysteriously drops all highlighting. 我发现有时我需要滚动以便调整语法高亮,因为有时它会神秘地丢弃所有突出显示。

Are there any work-arounds or ways to fix this? 是否有任何解决方法或方法来解决这个问题? I'm using vim 7.1. 我正在使用vim 7.1。

您可能希望尝试这种改进的Javascript语法高亮显示器,而不是VIMRUNTIME附带的那种。

Well, I've modified Yi Zhao's Javascript Syntax , and added Ajax Keywords support, also highlight DOM Methods and others. 好吧,我修改了Yi Zhao的Javascript语法 ,并添加了Ajax关键词支持,也强调了DOM方法等。

Here it is, it is far from being perfect as I'm still new to Vim, but so far it has work for me. 在这里,它远非完美,因为我还是Vim的新手,但到目前为止,它对我有用。 My Javascript Syntax . 我的Javascript语法 If you can fix, add features, please do. 如果您可以修复,添加功能,请执行。

UPDATE: I forgot these syntax highlights are only shown if you included them in your own colorscheme, as I did in my Nazca colorscheme. 更新: 我忘了这些语法高亮只有在你将它们包含在你自己的colorscheme中时才会显示,正如我在 Nazca colorscheme中所做的那样。 I'll test if I could add these line into my modified syntax file. 我将测试是否可以将这些行添加到我修改的语法文件中。

Follow the new version of the javascript syntax file in github , for it is no longer required to modify your current colorscheme. github中遵循新版本的javascript语法文件,因为不再需要修改当前的colorscheme。

Syntax coloring synchronization probably needs adjustment. 语法着色同步可能需要调整。 I've found in certain contexts that I need to change it. 我在某些情况下发现我需要改变它。

Syntax synchronization (":help syn-sync") controls how vim keeps track of and refreshes its parse of the code for coloring, so that it can start drawing anywhere in the file. 语法同步(“:help syn-sync”)控制vim如何跟踪和刷新其用于着色的代码解析,以便它可以开始在文件中的任何位置绘图。

The defaults don't always work for me, so sometimes I find myself issuing 默认值并不总是适用于我,所以有时我发现自己发布

:syn sync fromstart

I suggest reading through the documentation under 我建议阅读下面的文档

:help syn-sync

or just check 或者只是检查

:help syntax

and find the section on synchronization. 并找到有关同步的部分。

to make an informed decision among the four available basic options. 在四个可用的基本选项中做出明智的决定。 I maintain mappings to function keys to switch between "fromstart" and "ccomment" modes and for just clearing the sync settings. 我保持功能键的映射,以便在“fromstart”和“ccomment”模式之间切换,并且只清除同步设置。

This is a really old post, but I was experiencing the same thing: sometimes syntax highlight would just stop working when looking at the javascript section in an .html file. 这是一个非常古老的帖子,但我遇到了同样的事情:有时语法高亮只会在查看.html文件中的javascript部分时停止工作。 As the OP mentions, a quick workaround was to scroll up and then magically things would start highlighting again. 正如OP所提到的,一个快速的解决方法是向上滚动,然后神奇的事情将再次开始突出显示。

Today I found the underlying problem and a good solution. 今天我发现了潜在的问题和一个很好的解决方案。 In Vim, syntax highlighting uses a context to derive the correct highlight, where context is defined by the previous lines. 在Vim中,语法突出显示使用上下文来派生正确的突出显示,其中上下文由前面的行定义。 It is possible to specify how many lines before the current line are used by issuing :syntax sync minlines=200 . 可以通过发出:syntax sync minlines=200来指定当前行之前的行数:syntax sync minlines=200 In this case, it will use up to 200 previous lines as context. 在这种情况下,它将使用多达200个前面的行作为上下文。 It is possible to use the whole file (which can be slow for long files) by running :syntax sync fromstart . 通过运行:syntax sync fromstart ,可以使用整个文件(对于长文件来说可能很慢)。

Once I found that, I added this line to my .vimrc : 一旦我找到了,我将这行添加到我的.vimrc

autocmd BufEnter *.html :syntax sync fromstart

By doing so, .html files will use the whole file as context. 通过这样做, .html文件将使用整个文件作为上下文。 Thus, the javascript section will always by highlighted properly, regardless of how long the JS section is. 因此,无论JS部分有多长,javascript部分都将始终正确突出显示。 Hope this helps someone else out there! 希望这有助于其他人!

For a quick and dirty fix, sometimes I just scroll up and down and the highlighting readjusts. 对于快速而肮脏的修复,有时我只是向上和向下滚动并突出显示重新调整。 Ctrl + L for a screen redraw can also fix it. 按Ctrl + L进行屏幕重绘也可以修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM