Vim inoremap for specific filetypes

You need to do 2 things:

  • create a mapping local to a specific buffer by using the <buffer> option for inoremap.
  • load the mappings for just a specific filetype.

This can be done via an autocommand in your .vimrc like so:

autocmd FileType php inoremap <buffer> ( ()<Esc>i

The other way option is by creating a filetype plugin. (see :h ftplugin for more details)

A simple example is do create a file named, ~/.vim/after/ftplugin/php.vim and place your mappings inside like so:

inoremap <buffer> ( ()<Esc>i
inoremap <buffer> { {<CR>}<Esc>ko
inoremap <buffer> <? <?php ?><Esc><Left>i

I personally lean more towards the ftplugin approach but having a everything in your .vimrc file can be nice.

Leave a Comment