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:
- Check the first line of the file for
%&<format>
.
- Check the first 1000 non-comment lines for LaTeX or ConTeXt keywords.
- 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.