I have a set of small functions in VimL highlight a line (or a word) depending on certain conditions.
You should consider the workings of the functions to act similar as the spelling (:set spell
), underlining when the conditions are met.
I have found, however, that when the number of highlighted lines exceed about 75, there is a significant lag when moving. Either from side to side or up or down.
I had some convenient AutoCommands
that I was enabling by default, (for example, to echo why the line is highlighted) but even with all of them disabled, as soon as I call the function that highlights everything, I can tell there is a huge lag.
This is what I am using to highlight a word:
call matchadd('MyCheck', '^\%'. line . 'l\_.\{-}\zs\k\+\k\@!\%>' . column . 'c')
And this is what I use to highlight the whole line
call matchadd('MyCheck', '\%' . line . 'l\n\@!')
The 75 number I use as a reference for determining a lag is just a reference, it is a bit of a sweet spot for me, but just to demonstrate that most anything beyond gets increasingly worse.
I also use the SpellBad
highlighting for MyCheck
, but seriously doubt that this causes any problems.
Is there something I could do differently to avoid the lag? Is matchadd
the best option?
EDIT: Just to make sure it is not any of my functions or code doing something weird, I opened a 500 line file and did this:
highlight link MyCheck SpellBad
for line in range(line('$'))
call matchadd('MyCheck', '\%' . line . 'l\n\@!')
endfor
Which basically highlights every single line on the file. Everything clearly got impossibly slow.
Unsetting cursorline has a drastic (positive) effect in performance.
I did :set nocursorline
and now my movements (regardless of highlighting) are snappy as before.