40

I use Vim to write my .tex files, but I am having trouble with the spell checker in Vim. Sometimes it does not check the words, and I think that it might be for the following reason.

Since Vim is clearly not supposed to check all of the words in the .tex document, for example, not the preamble, it only check spelling in certain regions (in the syntax sense). As I have gathered from here, one of these regions is texSectionZone. These regions can become quite large, indeed a section often is, so Vim is having trouble realising that it actually is in a texSectionZone region (or in ay other), and therefore does not check the spelling. This can happen if I make a search in the document, or any kind of jump that skips multiple lines (or rather pages).

The way that I concluded this might be the reason is the following: I know that the command

:echo synIDattr(synID(line("."),col("."),1),"name")

prints the name of the region/regions you are in (I found it here), so when the spell checker did not work I tried this, and it told me that it was not in any region at all. The places it did work, I was in a region where it ought to check the spelling.

So far my only solution is to find the nearest section above the point I want the speller to check, and then manually move the cursor back down to the given point.

Ideally I would very much like a solution that ensures that this does not happen, but I would also settle for a way to manually make vim 'update' which region it is in, without me having to move the cursor a lot. In the latter case I am thinking of a solution which could be made to a shortcut.

PS I was in doubt about what to call the question. If you come up with a title which explain the problem better, fell free to change it.

Kristian
  • 1,667
  • 2
  • 15
  • 20

3 Answers3

62

Spell checking is not done where no syntax group are defined (or found by vim). I find it useful to enable spell checking even for the undefined group (see toplevel in vim's syntax documentation).

To do so, write the command:

syntax spell toplevel

in the file ~/.vim/after/syntax/tex.vim. Make the file if it does not exist.

Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
Dominik
  • 721
  • 1
  • 5
  • 3
  • 2
    Maybe obvious but it had me stumped; gotta use `set spell` before this will work. – Justapigeon Aug 17 '19 at 21:15
  • I didn't have a problem with tex files but did with yacc; thanks for this! BTW: you of course don't need this in a special file. You can have it in your `.vimrc` file as well after the setting of the spell option (as @Justapigeon mentioned). Whether doing it in `.vimrc` causes any other issues or is not the best way I do not know but for me it works fine - at least at first glance. Ah but it might be annoying in files that I don't want spellcheck in. Thanks for the tips anyway! – Pryftan Mar 24 '22 at 10:10
  • I added the line `autocmd FileType tex syntax spell toplevel` to my `.vimrc` and it works like a charm. Thanks! – Roshan Sam Aug 31 '23 at 11:43
22

syntax/tex.vim already uses quite elaborate sync patterns to ensure that the syntax highlighting is accurate, but for long and complex documents, this may still fail.

Best you can do is trying to increase both values of

syn sync maxlines=200
syn sync minlines=50

(e.g. to 2000 and 500). Put this in ~/.vim/after/syntax/tex.vim to override the defaults.

syntax sync fromstart

might give the best results, but may be too slow. You'll find a description of syntax syncing at :help :syn-sync.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • This was exactly what I was looking for. I tried to read in `:help sync-syn`, but I must admit that I do not really understand how things work. How does Vim update the syntax? Is a choice about this made somewhere. Maybe this is confusing. What I'm asking is probably: if I run `syntax sync fromstart` does it then update the syntax from the beginning of the document _once_, or does it tell Vim that it should always do syntax syncronizing from the beginning? – Kristian Apr 30 '14 at 09:57
  • 1
    The latter one; if this doesn't slow down Vim too much, stick to it! – Ingo Karkat Apr 30 '14 at 11:13
  • Okay. Most of the places on the net, where people suggest it, they make it sound as it is only once. If I run `:syn sync maxlines=200` and `:syn sync minlines=50` afterwards, do I then set it back to normal (as default)? – Kristian Apr 30 '14 at 11:51
  • This needs to be set only once, best in the _after_ directory mentioned in my answer. – Ingo Karkat Apr 30 '14 at 12:48
0

I manually set filetype :set filetype=plaintex and then vim spell works very well for my .tex documents, which are LaTeX files. many of my .tex files are automatically recognized as filetype=tex and when that happens, vim's spell will not check the spelling of a single word, even though most of the text are English sentences, frequently whole paragraphs, without any LaTeX syntax at all. I've also found that I can set filetype :set filetype=latex and that also works, but some of my custom commands \dosomething are not recognized as syntax, and they don't get a special font color... so I manually :set filetype=plaintex and at least I get the font color for my custom LaTeX commands in a special font color.

user12711
  • 693
  • 1
  • 7
  • 21