Vim配置特定的文件类型

Cur*_*urt 0 vim

我正在努力使用.vimrc来根据文件类型应用特定的配置.根据此处autocmd FileType建议,我尝试应用基于文件类型的配置.这是我在.vimrc中的内容:

autocmd FileType tex call Tex_config()
  function Tex_config()
    let g:LatexBox_viewer = 'skim'
    let g:LatexBox_latexmk_options = '-pvc'
    let g:tex_flavor = "latex"
    setlocal spell spelllang=en_ca
  endfunction
Run Code Online (Sandbox Code Playgroud)

我可以用以下函数调用函数Tex_config():debug Tex_config:Vim愉快地通过该函数.所以,一切似乎都应该有效.

但是,当我发出:set filetype=tex一些奇怪的事情时:拼写检查会关闭.当我发出:set filetype=foo拼写检查时.与我希望从此配置代码段发生的情况相反!

任何帮助将不胜感激.这是完整的vimrc(功能在44-50).谢谢.

Lyn*_*nch 5

您可以将特定命令添加到 .vim/ftplugin/tex.vim。然后,Vim 将自动识别 tex 是一种文件类型,并从 tex.vim 加载其行为。

例如,我将其用于手册页 .vim/ftplugin/man.vim:

setlocal nolist
setlocal nomod
setlocal nonumber

" Set scratch buffer"
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal noswapfile
Run Code Online (Sandbox Code Playgroud)

确保您的 vimrc 中有此行​​来设置您的 vim 文件夹:

let $VIM='~/.vim/'
Run Code Online (Sandbox Code Playgroud)


chi*_*gsy 5

不要将.vimrc用于特定于文件类型的东西,Lynch为文件类型或情境vimrc设置描述了一种令人难以置信的精确机制.

例如:我的.vim中的一些东西

.vim/ftdetect:
haml.vim       macports.vim   puppet.vim
hiveminder.vim markdown.vim   ruby.vim

.vim/ftplugin:
haml.vim             portfile.vim         sshconfig.vim
html_snip_helper.vim python.vim           tmux.vim
javascript.vim       python_match.vim     zsh.vim
markdown.vim         sass.vim
plist.vim            scss.vim
Run Code Online (Sandbox Code Playgroud)

比方说,我的sshconfig.vim文件(我设置了"K",为我提供了sshconfig的联机帮助页,其中包含我的光标所在的标题.好的,这个技巧.)

" sshconfig ftplugin. 
" At this point, only want to set man as the keywordprg

if exists("b:did_ftplugin")
    finish
endif
let b:did_ftplugin = 1

"  Customizations for sshconfig files
"  what I _want_ is to jump to the heading 
set keywordprg=man\ ssh_config\ -P\ \'less\ -p\ <cword>\'
Run Code Online (Sandbox Code Playgroud)

现在,在.vim/ftdetect/macports.vim中,我有代码来确定这个文件是否是一个Portfile

" Portfile
au BufNewFile,BufRead Portfile  set filetype=portfile
au FileType portfile compiler portfile
Run Code Online (Sandbox Code Playgroud)

我应该把那个编译器端口文件拉出来并放到ftplugins中,因为你可以看到这个系统会变得多么疯狂,但这就是ctags和git的用途.