开发者

Gvim doesn't highlight syntax correctly, how to fix it?

I have a HTML file which contains both HTML and JavaScript. When I folded some code snippets, the syntax hig开发者_C百科hlight didn't work well. Screenshot as following:

Gvim doesn't highlight syntax correctly, how to fix it?

Line79 function setColor(color) is not correctly highlighted. Is there any way to fix it?

BTW, I am using GVIM 7.2 in windows 7.

Thanks!


When Vim opens an html file it applies html highlighting throughout the file. When you have a separate language in the html file you need to define syntax regions to let Vim know that parts of the file are to be highlighted differently from the language identified by the file extension. I described how to do this in an answer to an SO question here: In VIM, how can I mix syntax/ident rules of both jinja and javascript in the same file?

Also review the docs at :h syn-include.

Indenting is similar. That is, Vim will apply html indenting rules to everything within the file unless you tell it to indent the Javascript region differently.

NOTE: Maybe since html with embedded javascript is so common the html syntax files may by default support embedded javascript. The tip linked below suggests using :set filetype htmlm4 to get proper highlighting, although a commenter says that should not be necessary: http://vim.wikia.com/wiki/Syntax_highlighting_for_HTML_with_embedded_Javascript


:syn sync fromstart

(See also help syn-sync-first)


It may be that you have a line in the mapInit() function whose length exceeds vim's 'synmaxcol' setting Notice the highlighting is incorrect on line 77 too (the closing brace).

set synmaxcol

From vim's help:

Maximum column in which to search for syntax items. In long lines the text after this column is not highlighted and following lines may not be highlighted correctly, because the syntax state is cleared. This helps to avoid very slow redrawing for an XML file that is one long line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜