<p>Has anyone else found VIM's syntax highlighting of Javascript sub-optimal? 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.</p>

<p>Are there any work-arounds or ways to fix this? I'm using vim 7.1.</p>

[{'answer_id': 58953, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 6, 'selected': True, 'text': '<p>You might like to try this <a href="http://www.vim.org/scripts/script.php?script_id=1491" rel="noreferrer">improved Javascript syntax highlighter</a> rather than the one that ships with VIMRUNTIME.</p>\n'}, {'answer_id': 59211, 'author': 'Thomas Kammeyer', 'author_id': 4410, 'author_profile': 'https://Stackoverflow.com/users/4410', 'pm_score': 4, 'selected': False, 'text': '<p>Syntax coloring synchronization probably needs adjustment.  I\'ve found in certain contexts that I need to change it.</p>\n\n<p>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.</p>\n\n<p>The defaults don\'t always work for me, so sometimes I find myself issuing</p>\n\n<pre><code>:syn sync fromstart\n</code></pre>\n\n<p>I suggest reading through the documentation under</p>\n\n<pre><code>:help syn-sync\n</code></pre>\n\n<p>or just check</p>\n\n<pre><code>:help syntax\n</code></pre>\n\n<p>and find the section on synchronization.</p>\n\n<p>to make an informed decision among the four available basic options.\nI maintain mappings to function keys to switch between "fromstart" and "ccomment" modes and for just clearing the sync settings.</p>\n'}, {'answer_id': 59593, 'author': 'AndrewJFord', 'author_id': 6154, 'author_profile': 'https://Stackoverflow.com/users/6154', 'pm_score': 2, 'selected': False, 'text': '<p>For a quick and dirty fix, sometimes I just scroll up and down and the highlighting readjusts. <kbd>Ctrl</kbd>+<kbd>L</kbd> for a screen redraw can also fix it.</p>\n'}, {'answer_id': 5652956, 'author': 'Jose Elera', 'author_id': 428786, 'author_profile': 'https://Stackoverflow.com/users/428786', 'pm_score': 4, 'selected': False, 'text': '<p>Well, I\'ve modified <a href="http://www.vim.org/scripts/script.php?script_id=1491">Yi Zhao\'s Javascript Syntax</a>, and added Ajax Keywords support, also highlight DOM Methods and others.</p>\n\n<p>Here it is, it is far from being perfect as I\'m still new to Vim, but so far it has work for me. <a href="http://www.vim.org/scripts/script.php?script_id=3425">My Javascript Syntax</a>. If you can fix, add features, please do.</p>\n\n<p>UPDATE: <strike>I forgot these syntax highlights are only shown if you included them in your own colorscheme, as I did in my <code>Nazca</code> colorscheme. I\'ll test if I could add these line into my modified syntax file.</strike></p>\n\n<p>Follow the new version of the javascript syntax file in <a href="https://github.com/jelera/vim-javascript-syntax">github</a>, for it is no longer required to modify your current colorscheme.</p>\n'}, {'answer_id': 40917247, 'author': 'jorgeh', 'author_id': 1620879, 'author_profile': 'https://Stackoverflow.com/users/1620879', 'pm_score': 3, 'selected': False, 'text': '<p>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 <code>.html</code> file. As the OP mentions, a quick workaround was to scroll up and then magically things would start highlighting again.</p>\n\n<p>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. It is possible to specify how many lines before the current line are used by issuing <code>:syntax sync minlines=200</code>. In this case, it will use up to 200 previous lines as context. It is possible to use the whole file (which can be slow for long files) by running <code>:syntax sync fromstart</code>.</p>\n\n<p>Once I found that, I added this line to my <code>.vimrc</code>:</p>\n\n<pre><code>autocmd BufEnter *.html :syntax sync fromstart\n</code></pre>\n\n<p>By doing so, <code>.html</code> files will use the whole file as context. Thus, the javascript section will always by highlighted properly, regardless of how long the JS section is. Hope this helps someone else out there!</p>\n'}]
['https://Stackoverflow.com/questions/58825', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1693/']