Jonatas Eduardo
Jonatas Eduardo

Reputation: 685

Vim running slow with LaTeX files

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.

Upvotes: 8

Views: 3711

Answers (5)

Maëlan
Maëlan

Reputation: 4192

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.

Upvotes: 0

nionios
nionios

Reputation: 75

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.

Upvotes: 1

gloriphobia
gloriphobia

Reputation: 1493

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.

Upvotes: 2

Ruslan
Ruslan

Reputation: 19120

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.

Upvotes: 1

Jonatas Eduardo
Jonatas Eduardo

Reputation: 685

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

Upvotes: 7

Related Questions