启动 Vim 时无法设置 `tex` 文件类型

5

我注意到如果我使用Vim打开一个不存在的HTML文件,文件类型会自动设置为html

$ vim foobar.html
:set filetype # --> filetype=html

另一方面,TeX不会发生同样的情况。如果我创建一个不存在的文件,则文件类型为plaintext,我必须保存并重新打开文件才能正确设置:
$ vim blabla.tex
:set filetype # --> filetype=plaintext

我也尝试了Python、C、VimL、Javascript文件,文件类型总是立即设置正确。我不知道为什么TeX文件没有这样的情况。
我唯一能想到可能会干扰的事情是vim-latex。这可能吗?如果是这样,我该如何解决这个问题?
如果有帮助的话,这里是我的不太长的.vimrc文件。
2个回答

9
Vim使用一种巨大的启发式算法来确定普通文本与其他变种Tex之间的区别。
如果您查看$VIMRUNTIME/filetype.vim,则会找到以下函数。
" 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 "latex" or to g:tex_flavor, can be set in user's vimrc.
func! s:FTtex()
  let firstline = getline(1)
  if firstline =~ '^%&\s*\a\+'
    let format = tolower(matchstr(firstline, '\a\+'))
    let format = substitute(format, 'pdf', '', '')
    if format == 'tex'
      let format = 'plain'
    endif
  else
    " Default value, may be changed later:
    let format = exists("g:tex_flavor") ? g:tex_flavor : 'plain'
    " Save position, go to the top of the file, find first non-comment line.
    let save_cursor = getpos('.')
    call cursor(1,1)
    let firstNC = search('^\s*[^[:space:]%]', 'c', 1000)
    if firstNC " Check the next thousand lines for a LaTeX or ConTeXt keyword.
      let lpat = 'documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>'
      let cpat = 'start\a\+\|setup\a\+\|usemodule\|enablemode\|enableregime\|setvariables\|useencoding\|usesymbols\|stelle\a\+\|verwende\a\+\|stel\a\+\|gebruik\a\+\|usa\a\+\|imposta\a\+\|regle\a\+\|utilisemodule\>'
      let kwline = search('^\s*\\\%(' . lpat . '\)\|^\s*\\\(' . cpat . '\)',
                              \ 'cnp', firstNC + 1000)
      if kwline == 1    " lpat matched
        let format = 'latex'
      elseif kwline == 2    " cpat matched
        let format = 'context'
      endif     " If neither matched, keep default set above.
      " let lline = search('^\s*\\\%(' . lpat . '\)', 'cn', firstNC + 1000)
      " let cline = search('^\s*\\\%(' . cpat . '\)', 'cn', firstNC + 1000)
      " if cline > 0
      "   let format = 'context'
      " endif
      " if lline > 0 && (cline == 0 || cline > lline)
      "   let format = 'tex'
      " endif
    endif " firstNC
    call setpos('.', save_cursor)
  endif " firstline =~ '^%&\s*\a\+'

  " Translation from formats to file types.  TODO:  add AMSTeX, RevTex, others?
  if format == 'plain'
    setf plaintex
  elseif format == 'context'
    setf context
  else " probably LaTeX
    setf tex
  endif
  return
endfunc

这个函数确定使用哪种tex的版本。如果你看一下它,你会发现默认值是从g:tex_flavor中获取的(如果存在,默认为plain)。如果你在vimrc中将这个变量设置为tex,vim将默认使用tex文件类型。

let g:tex_flavor = 'tex'

啊啊啊!我不知道它这么复杂。这实际上是正确的做法。加入那行代码解决了问题! - rubik

4

有一种方法可以自动化你手动进行的过程。

autocmd BufRead,BufNewFile *.tex set filetype=tex添加到你的.vimrc文件中,将会在你打开每个tex文件时自动设置tex文件类型。


谢谢!但是为什么Vim已经对除TeX以外的所有其他文件类型执行此操作了呢? - rubik
也许你的vim配置中没有这个内置功能。在我的系统上,它在这里:/usr/share/vim/vim74/filetype.vim我的vim无法以类似的方式识别go文件。 - pravj
我不知道... 不管怎样,我会将这个添加到我的.vimrc文件中,这样问题就解决了。 - rubik
很酷,不管怎样这正是vim识别这些文件类型的方式。你可以在我之前提到的文件中搜索,你会发现tex部分不像其他语言那样存在。vim-filetype - pravj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接