if (&filetype=='c' || &filetype=='cpp' || &filetype=='h')
  autocmd VimEnter * NERDTree
  autocmd VimEnter * wincmd p

  " class outline viewer : F8
  nmap <F8> :TagbarToggle<CR>

I tried this vimrc script, but it is not working.


Not sure but I think VimEnter is called before &filetype is set so that if is never executed. Try:

augroup C
    autocmd FileType c,cpp NERDTree | wincmd p | nmap <F8> :TagbarToggle<CR>
augroup END

Better to write an filetype plugin (see :h ftplugin). For example I have a file in ~/.config/nvim/after/ftplugin/javascript.vim with this content:

autocmd BufReadPost,BufWritePost <buffer> Accio eslint
autocmd InsertLeave,TextChanged <buffer> call semhl#highlight()
autocmd FileType javascript call semhl#highlight()
nnoremap <buffer> <C-]> :TernDef<cr>

These auto-commands and the mapping are only available for JavaScript files. I don't know how to call an ftplugin for both C and C++, but the content would be something like:

autocmd FileType c,cpp NERDTree | wincmd p
nmap <buffer> <F8> :TagbarToggle<CR>

If you use Vim the location of this file would be ~/.vim/after/ftplugin/c.vim.


According to this answer you can use symbolic links or source your c.vim ftplugin from your cpp.vim plugin (or vice versa).

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.