我注意到如果我用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.这有可能吗?如果是这样,我该如何解决这个问题呢?
解决方法
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'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。