Vim inoremap for specific filetypes Vim inoremap for specific filetypes php php

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>iinoremap <buffer> { {<CR>}<Esc>koinoremap <buffer> <? <?php ?><Esc><Left>i

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


Use autocmd FileType:

autocmd FileType php,c,java inoremap ( ()<Esc>i


put them in a this file

~/.vim/ftplugin/php.vim