8

I'm using Vim with a bunch of plugins (pathogen, ctags, snipmate, supertab, ...), and everything works fine for all kinds of file extensions.

However, when I'm try to edit .tex files it presents two problems which seem related. First, Vim starts to work really slow, and second, when I press "any letter + Tab", it tries to auto-complete with words previously written in the text.

One way which I tried to solve those issues, is by removing the supertab plugin from my bundle folder, but it's a not satisfactory solution.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Jonatas Eduardo
  • 655
  • 2
  • 9
  • 17

5 Answers5

7

The problem is due to the relativenumber option, when I turned it off the latex edit speed come back to normal.

Jonatas Eduardo
  • 655
  • 2
  • 9
  • 17
  • 2
    For others experiencing the same problem, and sluggish UI in latex files, the problem may be `cursorline` is on. By toggling it `set nocursorline`, the UI becomes fully responsive. I don't have a reason why this happens only with latex documents. – koo Jun 13 '12 at 04:01
2

The following relativenumber, cursorline and MatchParen can slow vim down a lot, especially when dealing with large latex files. When I turn them off then vim becomes much more responsive when dealing with large latex files.

To turn off relative number, type the following in editor mode:

:set nornu

To turn off cursorline, type the following in editor mode:

:set nocursorline

To turn off MatchParen, type the following in editor mode:

:NoMatchParen

If you still want regular line numbering then you can have

:set number

For a more permanent solution, you can also set latex specific settings in your ~/.vimrc file:

" Latex specification
au BufNewFile,BufRead *.tex
    \ set nocursorline |
    \ set nornu |
    \ set number |
    \ let g:loaded_matchparen=1 |

The \ and the | are there to allow you add the latex commands over multiple lines.

gloriphobia
  • 1,423
  • 13
  • 21
  • Oh wow, wow, this is aweful! `cursorline` had a huge impact for me. Any idea why this is happening. I feel like this really really shouldn't happen! – srs Sep 23 '20 at 11:43
1

The other two possible problems are the following being active

  1. cursorline
  2. DoMatchParen

So to make your LᴬTᴇX editing experience much better, you can do something like the following in your ~/.vimrc

au FileType tex setlocal nocursorline
au FileType tex :NoMatchParen

After doing this my Vim is as fast with .tex files as it is with .cpp ones.

Ruslan
  • 18,162
  • 8
  • 67
  • 136
1

I know this is an old issue, but, to anyone seeing this now, it is better to use the ftplugin/ folder in your .vim directory to instruct Vim to enable certain options on a specific file type. Create a file ~/.vim/ftplugin/tex.vim with the following options:

set nocursorline
set nornu
let g:loaded_matchparen=1

This makes Vim load these options only on TeX files (*.tex and related) without resorting to an :autocommand like gloriphobia’s answer does.

Maëlan
  • 3,586
  • 1
  • 15
  • 35
nionios
  • 75
  • 6
  • I think this should have been a comment on the mentioned answer, rather than an answer on its own (I also know that you don’t have enough rep to comment, sorry). – Maëlan Jun 10 '22 at 19:23
  • Thanks to your edit I can now comment everywhere hahaha). The author has not picked an accepted answer. Should I just comment on the question itself? – nionios Jun 20 '22 at 21:15
  • Great. :-) I’d say you put it below gloriphobia’s answer, although the same advice would apply to other answers as well. – Maëlan Jun 21 '22 at 08:58
0

Folding is another common source of slowdown. It is normally disabled by default, but perhaps you have enabled it. You can just disable it again:

" (1) if you use the builtin TeX support:
  " comment the line in your vimrc that looks like this:
  "let g:tex_fold_enabled = ...
  " OR, just to be sure, do:
  unlet! g:tex_fold_enabled

" (2) if you rather use VimTeX:
  " comment the line in your vimrc that looks like this:
  "let g:vimtex_fold_enabled = 1
  " OR, just to be sure, do:
  let g:vimtex_fold_enabled = 0

Note that folding must be enabled/disabled before TeX syntax is loaded in the buffer. Also, Vim option 'foldenable' (toggled by the normal-mode command zi) does not actually clear folding, it just hides it but it’s still there).

However, if you don’t want to give up on folding altogether, I found a single bottleneck in the builtin TeX folding that was responsible for most of the slowdown in my case: the document environment. Simple test: typing stuff just before \begin{document} is reasonably fast, but typing right after it is amazingly laggy. I guess it happens because that environment commonly spans hundreds of lines.

If you use the builtin TeX folding, you can prevent the folding of just the document environment by disabling the texDocZone matchgroup¹. Anyway, why would you want to fold the toplevel contents?

" put this in your vimrc :
au FileType tex :syntax clear texDocZone
" OR put this in ~/.vim/after/syntax/tex.vim :
syntax clear texDocZone

Alternatively, if you have VimTeX, you can replace the builtin TeX folding with the VimTeX’ one. I find it generally better, and it carefully avoids folding the document environment.

" put this in your vimrc :
unlet! g:tex_fold_enabled  " just to be sure
let g:vimtex_fold_enabled = 1

VimTeX’ folding is nicely customizable, see :help vimtex-folding.


¹ As of version 121 (April 2022) of the builtin TeX syntax.

Maëlan
  • 3,586
  • 1
  • 15
  • 35