4

I noticed that when I create a .tex from within Vim itself, in a later session that file is detected as plaintex, while already existing files are detected with the tex filetype (note: both of them have the .tex file extension).

I noticed it, since the syntax highlighting was different between the two.

I then created after\ftplugin\plaintex.vim in which I added

set filetype=tex

which does change the filetype, but it does not apply the relative syntax highlighting. I guess because this is done after the buffer is already loaded and processed.

I then tried with ftdetect/plaintex.vim with

setfiletype tex
set filetype=tex

It didn't work either.

I think the last option would be to work with either filetype.vim or script.vim to set filetype before built-in detection.

How can I make Vim assign the "tex" filetype when creating .tex files?

I've been trying to solve this myself, without success.

2
  • Do you have some kind of tex/latex package installed or is it vanilla Vim?
    – romainl
    Commented Nov 21, 2023 at 10:30
  • I only have Texlab LSP installed (using Neovim). So, I guess for the matter, it is like vanilla vim.
    – mEm
    Commented Nov 21, 2023 at 10:43

1 Answer 1

5

The filetype of a .tex file is determined by the content of the file.

In particular, if the first lines contains:

  • %&plaintex then it will be considered plaintex,
  • %&tex then it will be considered tex

If no %&format is found:

The filetype is by default set to:

  • plaintex if g:tex_flavor is set to plain or not defined
  • context if g:tex_flavor is set to context
  • tex otherwise

But the logic will further explore the first 1000 lines of the file for LaTeX or ConTeX constructions and set the filetype accordingly.

The list of LaTeX constructions in Vim 9.0 seems to be:

  • \documentclass
  • \usepackage
  • \begin{
  • \newcommnand
  • \renewcommand

The list of ConTeX constructions in Vim 9.0 seems to be:

  • \start
  • \setup
  • \usemodule
  • \enablemode
  • \setvariables
  • \useenconding
  • \usesymbol
  • \stelle
  • \verwende
  • \stel
  • \gebruik
  • \usa
  • \imposta
  • \regle
  • \utilisemodule

To have it set to tex instead of plaintex I see three solutions:

  1. Start the file with %&tex
  2. Set the g:tex_flavor to tex
  3. Make sure the content of the files contains \documentclass (or any LaTeX constructions)

To have it set to plaintex instead of tex I see two solutions:

  1. Start the files with %&plaintex
  2. Make sure the content of the file does not contain LaTeX constructions or ConTeX constructions.

More information with :help ft-tex-plugin

7
  • Thanks for the answer. Let me try the g:tex_flavor approach, if it works. But then, yet I'd love to understand the reasons of the difference in detection, for two similar file (i.e. both of them not including anyu \documentclass), just created differently.
    – mEm
    Commented Nov 21, 2023 at 14:13
  • I'm sorry Vivian, but it looks like this does not work as hoped. I double checked to be sure, but now setting "g:tex_flavor = "plain" does not prevent those same files to be still detected as "tex" filetype.. It seems to be working with tex_flavor = "latex" though... Anyway, I really appreciate your answer.
    – mEm
    Commented Nov 21, 2023 at 14:33
  • If you set g:tex_flavor to 'plain' but the content of the files contains \documentclass or LaTeX construction then Vim will consider it as LaTeX and will set the filetype as tex. Commented Nov 21, 2023 at 15:15
  • 1
    I have precised the answer. Let me know if it match your experience :-) Commented Nov 21, 2023 at 15:30
  • 1
    Thanks for the feedback :-) I hope it will also help others because the doc is quite vague about this ;-) Commented Nov 21, 2023 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.