问题
I\'ve added some simple inoremap
commands to my .vimrc
to help with parens and brackets completion, but I only want them to apply to php files.
inoremap ( ()<Esc>i
inoremap { {<CR>}<Esc>ko
inoremap <? <?php ?><Esc><Left>i
How to I set these commands to be active only when I\'m editing a .php file?
回答1:
Use autocmd FileType
:
autocmd FileType php,c,java inoremap ( ()<Esc>i
回答2:
You need to do 2 things:
- create a mapping local to a specific buffer by using the
<buffer>
option forinoremap
. - 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.
回答3:
put them in a this file
~/.vim/ftplugin/php.vim
回答4:
Add them to the ftplugin file in the after directory:
~/.vim/after/ftplugin/php.vim
More info in this answer.
Edit: This file, and maybe even the directory, will not be there by default. You may have to make it if you don't see it there.
回答5:
There may be a better way to do it, but this should work:
autocmd FileType php call Inoremaps()
fu! Inoremaps()
inoremap ...
endfu
来源:https://stackoverflow.com/questions/8825032/vim-inoremap-for-specific-filetypes