# Vim slow at rendering the buffer with LaTeX syntax highlighting

After fixing a hard-drive failure, I am installing GVIM on my Thinkpad X230T laptop running Windows 10. Things are working well except for the rending speed of buffers for *.tex files. The problem is: with syntax on, the buffer of latex renders exceptionally slow. Here goes a list of "features":

1. It takes seconds to refresh a buffer of size 1920*1080 (in pixels) when I press <C-f>;
2. If I shrink the size of the buffer, the rendering gets faster. Still, I shall experience a second lag at minimum. During the lag, I can only see a blank buffer.
3. Text operations are also slow. Simply starting a new line below by pressing o shall also end up with a one-second-lag. This is largely due to the rendering of the whole buffer after adding that single line: all the lines that follows shall be refreshed for their "new" position.

As sample screencast:

I have applied all the suggested options mentioned in this post (https://stackoverflow.com/questions/8300982/vim-slow-running-latex-files), yet I still get the same slow rendering speed.

I was only experiencing this exceptional slowdown on my X230T. With exactly the same _vimrc on the other two desktop machines, I don't even experience a tiny lag in the rendering speed. Nor did I experience the rendering problem with previous installation of Windows 7 and 8 on the same X230T laptop.

Lastly, for hard drive, I am having a SSD on my X230T, which worked pretty well so far; and I have HDD on both the other two desktop machines.

Thanks a lot!

All the best,

-Linfeng

• Did you figure this out in the end? I have the same on a x230t running OpenBSD. – Edd Barrett Sep 23 '16 at 10:49
• @EddBarrett, No, I have not. What I end up doing is to use Remote Desktop Connection on my X230T laptop to access my home desktop machine, which displays no delay at rendering. It may just be that X230T is too old for modern operating systems. (I definitely have no problem with rendering *.tex files when I was using Windows 7 and Windows 8.1). Good luck on your side. – llinfeng Sep 24 '16 at 1:33
• I find that turning syntax highlighting off helps. – Edd Barrett Sep 28 '16 at 13:03
• Definitely! Thanks a lot! I have been setting the filetype to null for a while :) – llinfeng Sep 28 '16 at 20:27
• Try opening the file with: vim -u NONE filename.tex to disable your .vimrc and other scripts from loading for the session. If this helps, see this method for finding out where exactly is the slow-down. – harrymc Oct 27 '16 at 16:30

The problem is, vim's regular expression engine is really slow, and I guess latex is pretty demanding on regex.

I was able to get vim a bit faster by doing:

:syn clear texSectionFold
:syn clear texPreamble


These were the main offending syntax group regexes.

I found them by doing:

:syntime on


Now press ctrl+L a ton of times and wait for vim to catch up. Then:

:syntime report


This gave the following output:

  TOTAL      COUNT  MATCH   SLOWEST     AVERAGE   NAME               PATTERN
8.903872   1911   52      0.017387    0.004659  texSectionFold     \v%(%(\\begin\{document\}.*$\n)@<=^|\\section) 4.979438 1859 0 0.016382 0.002679 texPreamble \v%(\\documentclass)@= 0.634906 1976 182 0.010863 0.000321 texEnvName \v%(\\%(begin|end)\{)@<=\a+\*?\ze\} 0.373173 1859 0 0.000880 0.000201 texArgsEnvNormReq \v(\\begin\{%(theorem|lemma|proposition|corollary|conjecture|definition|remark|example|proof)\*?\}\s*)@<=\{ 0.317732 1859 0 0.000468 0.000171 texArgsEnvNormOpt \v(\\begin\{%(theorem|lemma|proposition|corollary|conjecture|definition|remark|example|proof)\*?\}\s*)@<=\[ 0.223595 1859 0 0.000341 0.000120 texDimen \v-?%(\.[0-9]+|([0-9]+(\.[0-9]+)?))%(pt|pc|bp|in|cm|mm|dd|cc|sp|ex|em)> ...  So you can see that those two regexes are way more expensive than others. I don't use folds, so I had no issue disabling that. Not sure why finding the preamble is so slow. Perhaps this helps you too, @llinfeng. I hope so! EDIT: To run those syn clear commands when vim starts, you would need to put them in ~/.vim/after/syntax/tex.vim. It won't work in an ftplugin file, as the syntax definitions are not yet loaded at that point. EDIT2: Turns out some of the syntax groups I posted above came from a plugin. Disabling the plugin doesn't help, as then some other syntax groups are slow. Eesh! My final fix, and all I ask from syntax highlight, is: $ cat ~/.vim/after/syntax/tex.vim
syn clear
syn match texComment   "%.*\$"


It's really fast.

• Hi @Edd, thanks a lot! My syntime report is more unfriendly, as you may see here dropbox.com/s/guxzuqcuu2p510n/syntime_report_Tex.png?dl=0 The difference is that, my slow-down of rendering may be attributed to literally all display options. I will now look for a way to only "paint color" for math expressions. – llinfeng Nov 22 '16 at 20:14
• Glad that helped. I still feel like something is wrong with vim if these modern machines can't run a few regexs. FWIW, I also tried with neovim, and it was the same there. – Edd Barrett Nov 23 '16 at 10:23
• Hi @Edd, here goes the minimum syntax highlighting file that I created, it has more color in it (yet, as I don't have much understanding of the syntax highlighting, it may be crappy in all possible ways): github.com/llinfeng/Vim/blob/X230T/vimfiles/after/syntax/… – llinfeng Feb 5 '17 at 21:13

You have used vim -u NONE filename.tex when opening the file to disable your .vimrc and other scripts from loading for the session, and this has fixed the problem.

The conclusion is that something started by the .vimrc file causes the slow-down.

Try to selectively delete parts of the file to find out what exactly is the cause. Remember to take a backup.

• As mentioned above, it is the syntax colouring built into vim that causes the issue, not a plugin. – Edd Barrett Nov 3 '16 at 13:26
• @EddBarrett: I am confused between you and llinfeng. He says that without .vimrc syntax highlighting is fast enough. I don't know what causes the slow-down in his .vimrc, but you could check if it's the same with yours. I cannot check it, as I don't use latex. – harrymc Nov 3 '16 at 15:14
• Without .vimrc you don't get any syntax highlighting, You need :set syntax=on in the vimrc. – Edd Barrett Nov 4 '16 at 17:31
• @EddBarrett: One can probably turn this on also during execution, but one good test could be to use a .vimrc that just has this line. In any case, I counseled recreating the file in stages until the problem reappears (if it truly does disappear as I was told). – harrymc Nov 4 '16 at 18:55

# Partial Solution for Thinkapd X230T per se

## Problem Identification

As @Edd has suggested, use :syntime on and syntime report to identify the most time-consuming syntax-highlighting "group/region/match"';

## Partial Solution

As to be found in the following URL, I have loaded the tex.vim in the after directory of my Vim distribution (c:\vim\vimfiles\after\syntax\ for my case):

https://github.com/llinfeng/Vim/blob/X230T/vimfiles/after/syntax/tex.vim

## Outcome

Using the same LaTeX file, my improved performance goes as follows

## Open question --- how to replicate the syntax-highlighting effeciently

The syntax coloring is not ideal, as compared to the native tex.vim to be found as c:\vim\vim80\syntax\tex.vim.