Enabling certain plugins and options for .h and .cpp files only in Vim

前端 未结 2 1856
广开言路
广开言路 2021-01-28 00:54

I have delimitMate installed for brace completion in Vim but it is running for all files, not just .h and .cpp files. DelimitMate has an option for disabling itself in the buffe

相关标签:
2条回答
  • 2021-01-28 01:38

    Reading the DelimitMate documentation at :h 'b:delimitMate_autoclose'. Add the following to your ~/.vimrc:

    let g:delimitMate_autoclose = 0
    augroup my_delimitmate
      autocmd!
      autocmd FileType cpp let b:delimitMate_autoclose = 1
    augroup END
    

    The idea is to turn off autoclose globally (g:) and turn it back on for specific buffers (b:).

    Instead of the :autocmd and :augroup commands you can set b:loaded_delimitMate inside the appropriate ftplugin file. Example add the following to ~/.vim/after/ftplugin/cpp.vim:

    let b:delimitMate_autoclose = 1
    

    This method might be prefered if you want to keep your ~/.vimrc file clean or you already have many filetype specific commands, settings, or mappings.

    Note: I do not use DelimitMate so I have not tested any of these settings.

    For more help see:

    :h :aug
    :h :au
    :h FileType
    :h 'b:delimitMate_autoclose'
    :h delimitMateFileType
    :h ftplugin
    
    0 讨论(0)
  • 2021-01-28 01:38

    I think what you are looking for is autocommands. Autocommands can run pieces of vimscript whenever a certain event happens. You can see a list of all possible events by doing :help autocmd-events

    So according to your needs, first you should disable delimitMate in your vimrc. Then, you can put something like this in your vimrc (after you disable delimitMate):

    autocmd FileType cpp (insert command to disable delimitMate here)
    

    That way, vim will disable delimitMate by default, but it will enable it if the fieltype is cpp. (this includes header files) Of course, more information is available on the autocmd command by doing :help :autocmd

    0 讨论(0)
提交回复
热议问题