Vim在启动时没有设置`tex`文件类型

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

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

另一方面,TeX也不会发生同样的情况.如果我创建一个不存在的文件,文件类型是纯文本,我必须保存文件并重新打开它以正确设置:

$vim blabla.tex
:set filetype # --> filetype=plaintext

我也试过Python,C,VimL,Javascript文件,文件类型总是立即设置.我不知道为什么TeX文件不会发生这种情况.

我能想到的唯一可能是干扰的是vim-latex.这有可能吗?如果是这样,我该如何解决这个问题呢?

如果它可能有帮助,here是我不太长的.vimrc文件.

解决方法

Vim使用巨型启发式方法来确定plaintex与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 . '\)',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).如果将此变量设置为tex(在vimrc中),则vim将认为tex文件类型.

let g:tex_flavor = 'tex'

相关文章

解决方案:解决linux下vim乱码的情况:(修改vimrc的内容)全...
Linuxvi/vim所有的UnixLike系统都会内建vi文书编辑器,其他的...
      vim正则匹配:空行:/^$/  /^[\t]*$/注释...
$select-editorSelectaneditor.Tochangelater,run'sele...
上次手贱忘了保存,这次就简单做个备忘吧,把踩过的坑记一下...
Linux之文本编译器小结vim的优势所有的UNIX-LIKE习通都会内置...