Toran Billups
Toran Billups

Reputation: 27399

Can you dynamically set a vim shortcut based on file type?

I have a custom "go to definition" shortcut that works great in python (using ropevim) but when I'm in a coffee-script file I need to use the normal ctags lookup to navigate around. Would it be possible to make just one shortcut that did either a ropevim lookup OR a ctags lookup depending on the file type that is open in the current buffer?

Upvotes: 2

Views: 501

Answers (1)

romainl
romainl

Reputation: 196566

You can put if/endif tests in your mappings by way of :execute but that's a bit messy.

What you should do instead is define your mappings in filetype-specific autocmds (see :h autocmd):

autocmd FileType python nnoremap this that

or, better, define them in ~/.vim/after/ftplugin/python.vim (and another file for coffeescript and so on):

nnoremap <buffer> this that

Upvotes: 6

Related Questions