Vim在启动时没有设置`tex`文件类型
发布时间:2020-12-16 01:50:32 所属栏目:安全 来源:网络整理
导读:我注意到如果我用Vim打开一个不存在的 HTML文件,文件类型会自动设置为html: $vim foobar.html:set filetype # -- filetype=html 另一方面,TeX也不会发生同样的情况.如果我创建一个不存在的文件,文件类型是纯文本,我必须保存文件并重新打开它以正确设置: $v
|
我注意到如果我用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 = 'starta+|setupa+|usemodule|enablemode|enableregime|setvariables|useencoding|usesymbols|stellea+|verwendea+|stela+|gebruika+|usaa+|impostaa+|reglea+|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' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
