# Starting vimtex plugin from within a vim session

I use Vimtex to edit LaTeX documents in Vim. If I open an existing .tex file, the Vimtex plugin will be initiated automatically. However, if I create a new .tex file, there's no way to initiate the plugin. I have to start a new vim sessions altogether, which is quite annoying when there are multiple tabs open. e.g.

cd ~ && vim delme.tex


starts a vim session without initiating the plugin. Similarly if I do :tabe delme2.tex from within that session.

How do I start up the Vimtex plugin (or any other plugin) from within a Vim session?

This happens because only by the .tex file extension, Vim doesn't know whether a file is of type LaTeX or Plain TeX and it defaults to the latter.

The filetype detection logic is defined in function dist#ft#FTtex() from script \$VIMRUNTIME/autoload/dist/ft.vim, which is preceded by the following comments:

Choose context, plaintex, or tex (LaTeX) based on these rules:

1. Check the first line of the file for %&<format>.
2. Check the first 1000 non-comment lines for LaTeX or ConTeXt keywords.
3. Default to plain or to g:tex_flavor, can be set in user's vimrc.

This explains why a new blank file will default to using Plain TeX, which is most likely not what you want.

You can override that manually with :set ft=tex after Vim detects your file incorrectly. This should fix it for the current open buffer. Also note that the names used for the 'filetype' setting are ft=tex for LaTeX and ft=plaintex for Plain TeX.

Another option is to either enter a first line with %&latex or similar (rule #1 above.) Or enter a keyword recognized as LaTeX, among which are \documentclass, \usepackage and \begin{...} (rule #2 above.) Using either of the two, save the file with :w and re-read it with :e. While re-reading it, Vim will do filetype detection again and this time it should properly set ft=tex (for LaTeX) automatically for you.

Third possibly is, if you typically only edit LaTeX files and would like to make that the default (using rule #3), by appropriately setting the g:tex_flavor variable on your vimrc, with a line such as:

let g:tex_flavor = 'latex'


Once that setting has been loaded, then every new *.tex file (or an existing one that isn't detectable by rules #1 and #2) will default to using ft=tex, which means LaTeX.

• Very thorough, thanks a lot. :set ft=tex is exactly what I was looking for! The options are equally as handy. – algae May 13 '20 at 11:37