Vim inoremap for specific filetypes

后端 未结 5 2002
孤独总比滥情好
孤独总比滥情好 2020-11-29 11:38

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.

相关标签:
5条回答
  • 2020-11-29 11:51

    Use autocmd FileType:

    autocmd FileType php,c,java inoremap ( ()<Esc>i
    
    0 讨论(0)
  • 2020-11-29 11:52

    put them in a this file

    ~/.vim/ftplugin/php.vim
    
    0 讨论(0)
  • 2020-11-29 12:07

    There may be a better way to do it, but this should work:

    autocmd FileType php call Inoremaps()
    fu! Inoremaps()
       inoremap ...
    endfu
    
    0 讨论(0)
  • 2020-11-29 12:09

    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.

    0 讨论(0)
  • 2020-11-29 12:09

    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.

    0 讨论(0)
提交回复
热议问题