This could be an issue with vim's system for recognising and then highlighting syntactic "keywords" not getting enough context to recognise keywords.
The basic concepts of vim's syntax system (finding and tagging basic syntax elements) is explained in
Basically vim uses regex to recognise syntactic elements in a file, once these are identified, then highlighting rules are applied to colorise those code constructs, (i.e. to do "syntax highlighting"), but as explained in
:h 44.10 vim doesn't start parsing from the beginning of the file, it starts some number of lines (e.g. 10 or 100 lines) upward from the top of your current viewport.
If a syntactic construct, e.g. an opening HTML tag or a function definition etc starts higher than the point vim starts parsing, vims syntax regex breaks.
Usually to fix this you increase the point that vim starts parsing.
To find out how many lines above the top line of the viewport vim is starting its parsing use:
Here is my output when viewing an HTML file:
--- Syntax sync items ---
syncing on items; minimal 10, maximal 100 lines before top line
[lots of other output here...]
if I had a problem with syncing - just for that file - I would increase
:syntax sync minlines=200
If you repeatedly get this problem fixing it is a bit harder. This is because the sync value is often set in a syntax file dedicated to the filetype you are editing, e.g. for me
syntax sync minlines for html filetypes is set in the syntax file
syntax parsing and config values for css files is in
shell is in
sh.vim and so on.
These syntax scripts only run at the time you open a file of the type
baz.sh etc, so setting a line in your vimrc won't work because these scripts would blow your settings away, one solution is to use an autocmd which triggers when you enter the file
autocmd BufEnter *.html :syntax sync minlines=200