0

How can I group all cscope (:cs) related configuration into e.g. autocmd, so that this configuration is only applied to c or cpp files?

I have been looking at e.g. this, but it only address a single filetype, and it only applies autocmd to mappings (not :set or :if statements):

augroup pscbindings
  autocmd! pscbindings
  autocmd Filetype purescript nmap <buffer> <silent> K :Ptype<CR>
  autocmd Filetype purescript nmap <buffer> <silent> <leader>pr :Prebuild!<CR>
augroup end

cscope configuration in ~/.vimrc:

if has("cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set nocst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
         cs add cscope.out
   " add database pointed to by environment
   elseif $CSCOPE_DB != ""
         cs add $CSCOPE_DB
   endif
   set csverb

   noremap <C-[> :cstag <C-R>=expand("<cword>")<CR><CR>

   nnoremap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR>
   nnoremap g<C-]> :cs find s <C-R>=expand("<cword>")<CR><CR>
   nnoremap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
   nnoremap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR>
   nnoremap g<C-\> :cs find c <C-R>=expand("<cword>")<CR><CR>
   nnoremap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR>
   nnoremap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR>
   nnoremap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
   nnoremap <C-_>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
   nnoremap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR>
   nnoremap <C-_>a :cs find a <C-R>=expand("<cword>")<CR><CR>
endif
0
4

The answer you linked is the right direction: ftplugins are made exactly for this purpose, check the doc: (:h ftplugin)

If you have things that you want used in both c and cpp ftplugins you could put it in a function in ~/.vim/autoload/cscope.vim and then in ~/.vim/after/ftplugin/c.vim and ~/.vim/after/ftplugin/cpp.vim you can simply call this function.

There are a few things you'll need to pay attention to:

  • You probably want your mappings to be buffer local, so you'll want to use :h :map-<buffer>
  • The options that you set are all global so either you want to keep them on your vimrc or you have different settings for them in different filetype and then you'll need to create buffer-local autocommands :h autocmd-buflocal to change the values when you navigate between buffers.

I think that this article From .vimrc to .vim has some useful information too.

3
  • Thank you. How can an autocmd span multiple lines like in if-then-else statement? Do I need to use | to separate the differents parts (e.g. if, endif, etc.)?
    – Shuzheng
    Apr 14 at 12:40
  • I think a good rule of thumb is that when your autocmd needs to be multiline it's better to put its action in a function and to call the function in the autocmd. But if you really need to have multiline you can put a `` at the beginning of the second line like in most vimscript scripts.
    – statox
    Apr 14 at 12:58
  • Can you provide a link to an example? Although, your explanation provides all the essentials (I guess), it requires more knowledge of vim than I currently have.
    – Shuzheng
    May 20 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.