我注意到如果我用Vim打开一个不存在的HTML文件,文件类型会自动设置为html:
$ vim foobar.html
:set filetype # --> filetype=html
Run Code Online (Sandbox Code Playgroud)
另一方面,TeX也不会发生同样的情况.如果我创建一个不存在的文件,文件类型是plaintext,我必须保存文件并重新打开它以正确设置:
$ vim blabla.tex
:set filetype # --> filetype=plaintext
Run Code Online (Sandbox Code Playgroud)
我也试过Python,C,VimL,Javascript文件,文件类型总是立即设置.我不知道为什么TeX文件不会发生这种情况.
我能想到的唯一可能会干扰的是vim-latex.这有可能吗?如果是这样,我该如何解决这个问题呢?
如果它可能有帮助,这是我不太长的.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 . '\)', '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
Run Code Online (Sandbox Code Playgroud)
此函数确定要使用的tex风格.如果你看它,你可以看到默认值取自g:tex_flavor(如果存在,默认为plain).如果将此变量设置为tex(在vimrc中),则vim将默认为tex文件类型.
let g:tex_flavor = 'tex'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1116 次 |
| 最近记录: |