How can I configure Vim for 2 different languages?

I am currently using Vim for Python, and would like to start using it while I learn Ruby too.

Is there a way to configure the vimrc file so that different settings apply depending on the filetype currently being worked on?

For example, my vimrc is currently set to have an indentation of 4 spaces, which I would like to be 2 spaces for Ruby files. Additionally, I would like the syntax Ruby syntax highlighting to be on when working on ruby files, and Python syntax highlighting for python files.

I stumbled across this for defining the tab spaces:

autocmd FileType python set tabstop=8|set shiftwidth=4|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab

Is there something similar for syntax highlighting?


First,

make sure you have the following lines somewhere near the top of your vimrc :

filetype plugin indent on
syntax on

Second,

this snippet is technically correct:

autocmd FileType python set tabstop=8|set shiftwidth=4|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab

but it could be made:

  • simpler and more readable by removing the bars and the redundant set s,
  • safer by changing the remaining ones to setlocal in order to limit your options to the targeted buffer:

    autocmd FileType python setlocal tabstop=8 shiftwidth=4 expandtab
    autocmd FileType ruby   setlocal tabstop=8 shiftwidth=2 expandtab
    
  • Third,

    those autocommands don't replace themselves when you re-source your vimrc : they will just pile up, and up, and up… until your Vim becomes unbearably slow and unresponsive.

    If you insist on keeping those settings in your vimrc it would be wise to use the pattern described by Cody in his answer:

    augroup python
        autocmd!
        autocmd FileType python setlocal tabstop=8 shiftwidth=4 expandtab
    augroup END
    
    augroup ruby
        autocmd!
        autocmd FileType ruby setlocal tabstop=8 shiftwidth=2 expandtab
    augroup END
    

    Fourth,

    Vim's filetype detection mechanism already does most of the work for you by looking for ftplugin/python.vim and after/ftplugin/python.vim in &runtimepath each time the FileType event is triggered with the value python … which makes adding FileType autocommands to your vimrc largely redundant.

    Keep your vimrc lean and clean by creating the file after/ftplugin/python.vim with this content:

     setlocal tabstop=8
     setlocal shiftwidth=4
     setlocal expandtab
    

    and so on for ruby and other filetypes…

    Note: use ftplugin/python.vim if you want to completely override the default python filetype plugin and after/ftplugin/python.vim if you only want to add/change a few things.

    Note: paths are relative to ~/.vim on unix-like systems and %userprofile%vimfiles on Windows.


    augroup ruby
      autocmd!
      autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab
      ... Any other ruby specific settings
    augroup END
    
    augroup python
      autocmd!
      autocmd FileType python set tabstop=8|set shiftwidth=4|set expandtab
      ... Any other python specific settings
    augroup END
    

    In the case of syntax highlighting, it should be happening automatically. If vim isn't detecting the file type for you, :setf ruby or :setf python should work while you're in the file.

    链接地址: http://www.djcxy.com/p/49454.html

    上一篇: 在Vim中,选择,删除或注释大部分多行的最佳方式是什么

    下一篇: 我如何配置2种不同语言的Vim?