Javascript syntax highlighting in vim

Has anyone else found that VIM syntax highlighting for Javascript is not optimal? I find that sometimes I need to scroll to adjust the syntax highlighting, as sometimes it mysteriously throws the entire selection.

Are there any problems or ways to fix this? I am using vim 7.1.

+43
javascript vim editing vim-syntax-highlighting
Sep 12 '08 at 12:32
source share
5 answers

You might like to use the enhanced Javascript syntax shortcut , rather than the one that comes with VIMRUNTIME.

+29
Sep 12 '08 at 13:28
source share

Well, I modified Yi Zhao Javascript Syntax and added Ajax Keywords support, also highlighted DOM Methods and others.

Here it is, it’s far from ideal, as I am still new to Vim, but so far it works for me. My Javascript Syntax . If you can fix, add features please.

UPDATE: I forgot that these syntax flashes are only displayed if you included them in your own color scheme, as it was in my Nazca color scheme. I will check if I can add this line to my modified syntax file.

Follow the new version of javascript syntax file on github as you no longer need to change the current color scheme.

+15
Apr 13 2018-11-11T00:
source share

Synchronization synchronization synchronization probably needs to be configured. I found in certain contexts that I need to change it.

Syntax synchronization (": syn sync sync") determines how vim tracks and updates its code analysis for coloring so that it can start drawing anywhere in the file.

By default, they don’t always work for me, so sometimes I find that I release

 :syn sync fromstart 

I suggest reading the documentation under

 :help syn-sync 

or just check

 :help syntax 

and find the sync section.

make an informed decision among the four basic options available. I support the display of function keys to switch between "fromstart" and "ccomment" modes and just to clear the synchronization settings.

+12
Sep 12 '08 at 15:04
source share

For a quick and dirty fix, sometimes I just scroll up and down and adjust the backlight. Ctrl + L to redraw the screen can also fix it.

+3
Sep 12 '08 at 17:40
source share

This is a really old article, but I experienced the same thing: sometimes the syntax highlighting just stopped working when I looked at the javascript section in the .html file. As the OP mentions, a quick workaround was to scroll up and then the magic would start to stand out again.

Today I found the main problem and a good solution. In Vim, syntax highlighting uses context to get the correct highlight, where the context is determined by the previous lines. You can specify how many lines before the current line are used in the output :syntax sync minlines=200 . In this case, it will use up to 200 previous lines as context. You can use the entire file (which can be slow for long files) by running :syntax sync fromstart .

As soon as I found this, I added this line to my .vimrc :

 autocmd BufEnter *.html :syntax sync fromstart 

This way .html files will use the whole file as context. This way, the javascript section will always be highlighted correctly, regardless of how long the JS section takes. Hope this helps someone else!

0
Dec 01 '16 at 17:52
source share



All Articles