27

I open .tex files with Vim, but some files are recognized as filetype=plaintex and others as filetype=tex. Why is this? What is the trigger that recognizes the file as tex, which has the highlighting I want?

Matthew Rankin
  • 5,240
  • 3
  • 26
  • 25
Stefano Borini
  • 2,294
  • 6
  • 34
  • 49

3 Answers3

30

Why are some .tex files opened in Vim as filetype=plaintex and others as filetype=tex? Since the same extension, .tex in this case, is used for multiple filetypes, "Vim tries to guess what kind of file it is" according to the Vim filetype-overrule documentation.

The ft-tex-plugin section of the Vim filetype documentation lists the rules used by Vim (versions 7 and higher) to determine which filetype to use for .tex files:

  1. If the first line of the file is %&<format> where <format> can be plain, context, or latex, then the filetype is set to plain TeX, ConTeXt, or LaTeX, respectively.
  2. If there is no format designator command on the first line, then Vim searches the file for keywords to determine if the filetype should be set to context (ConTeXt) or tex (LaTeX).
  3. If no keywords are found, the filetype is set to plaintex (plain TeX).

The last rule is important. If you create an empty .tex file using a command like touch myfile.tex, then when you open it in Vim the filetype will default to plaintex, since the file is blank.

You can change the default behavior by setting the global variable tex_flavor in your .vimrc:

  • let g:tex_flavor = "plain"
  • let g:tex_flavor = "context"
  • let g:tex_flavor = "latex"
Skippy le Grand Gourou
  • 2,085
  • 1
  • 21
  • 19
Matthew Rankin
  • 5,240
  • 3
  • 26
  • 25
14

Are you using the LaTeX-suite plugin? If so, then you probably want

let g:tex_flavor='latex'

in your .vimrc.

frabjous
  • 10,755
  • 3
  • 34
  • 27
  • This has nothing to do with the LaTeX-suite plugin. See answer by Matthew Rankin or [`:h ft-tex-plugin`](http://vimhelp.appspot.com/filetype.txt.html#ft-tex-plugin). – Hotschke May 25 '18 at 15:28
  • Certain plugins may not work with `plaintex`, which is the default value if no keyword is found. I find the setting in this answer helpful to consistently set `tex` to be the filetype for a newly created `.tex` file. This helps Vimtex to work with the newly created file under the correct filetype. – llinfeng May 28 '20 at 00:54
3

More than I ever wanted to know about this subject: http://vimdoc.sourceforge.net/htmldoc/filetype.html

To disable one of the file types, add a line in your filetype file, see |remove-filetype|.

RedGrittyBrick
  • 81,981
  • 20
  • 135
  • 205