开发者

Vim config for specific filetypes

I'm struggling with using .vimrc to apply specific configurations based on the filetype. Following the autocmd FileType suggestion here, I have attempted to apply a configuration based on filetype. Here is what I have in my .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

I can call the function Tex_config() with :debug Tex_config: Vim happily spets through the function. So, everything seems like it should work.

But, when I issue :set filetype=tex something strange happens: spell checking turns off. And when I issue :set filetype=foo spell checking turns on. Just the reverse of what I expect to happen from this configuration snippet!

Any assistance would be greatly appreciated. Here is the full vimrc (function at 44-50). Thanks.


Don't use .vimrc for filetype specific things, there is an incredibly precise mechanism that Lynch described for filetype or situational vimrc settings.

For example: some of the stuff in my .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

and in say, my sshconfig.vim file ( I've set "K" to give me the manpage for sshconfig with at the heading that my cursor is at. Sofaking good, that trick. )

" 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>\'

Now, in .vim/ftdetect/macports.vim, I have the code to determine if this file is a Portfile

" Portfile
au BufNewFile,BufRead Portfile  set filetype=portfile
au FileType portfile compiler portfile

I should pull that compiler portfile out and drop it into ftplugins, because you can see how this system would get quite crazy, but that is what ctags and git are for.


You can add your specific command to .vim/ftplugin/tex.vim. Vim will then automatically recognize that tex is a file type and load its behavior from your tex.vim.

For example I use this for man pages .vim/ftplugin/man.vim:

setlocal nolist
setlocal nomod
setlocal nonumber

" Set scratch buffer"
setlocal buftype=nofile
setlocal bufhidden=hide
setlocal noswapfile

Make sure you have this line in your vimrc to set your vim folder:

let $VIM='~/.vim/'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜