5

I'm trying to setup mappings for <C-]> that, depending on the buffer's file type, trigger different functions. For example, if I'm in a Javascript buffer, I want <C-]> to trigger :TernDef. If I'm in a C buffer, I want it to trigger :YcmCompleter GoTo. I tried autocmd FileType c nnoremap <C-]> :YcmCompleter GoTo<CR> but it just seems to ignore that.

  • 6
  • 2
    First have you tried to make your mapping working without the autocmd, just to be sure that it does work (if it doesn't @muru gave you a link to a question of mine which should help you). Also I think that you want to use <buffer> in your mapping: In your current command when you open a c file <c-]> will be remapped for all of your buffer (C or not). with <buffer> your mapping will exist only on C buffers, see :h :map-<buffer>. – statox Dec 20 '16 at 9:05
17

That autocmd looks fine; however, you should define buffer-local mappings (with <buffer>); else, editing one type of file will override the other type's (global) mapping:

autocmd FileType c          nnoremap <buffer> <C-]> :YcmCompleter GoTo<CR>
autocmd FileType javascript nnoremap <buffer> <C-]> :TernDef<CR>

Critique of the autocmd approach

You can define mappings for certain filetypes by prepending :autocmd Filetype {filetype} ..., and put that into your ~/.vimrc. But that gets unwieldy as you add mappings and other settings for various filetypes. Better put the :map commands into ~/.vim/ftplugin/{filetype}_mappings.vim. (This requires that you have :filetype plugin on.)

0

For me that line does work (using commands that my Vim recognizes and including a colon at the start of the command). You could try to start Vim without plugins and see whether the line is recognized properly then.

Another option would be to put the commands you want to run in the c.vim file in the ftplugin directory. This way they will be run when you open a file of that type. More info can be found in :help ftplugin<cr>.

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.