Automatically insert a matching brace in Vim

前端 未结 18 2591
醉梦人生
醉梦人生 2020-12-13 05:23

I spend way too much time fumbling around because Vim doesn\'t handle closing braces like most IDEs do. Here\'s what I want to happen:

Type this:

         


        
18条回答
  •  无人及你
    2020-12-13 06:12

    My solution:

    inoremap   InsertMapForEnter()
    function! InsertMapForEnter()
        if pumvisible()
            return "\"
        elseif strcharpart(getline('.'),getpos('.')[2]-1,1) == '}'
            return "\\O"
        elseif strcharpart(getline('.'),getpos('.')[2]-1,2) == '\O"
        else
            return "\"
        endif
    endfunction
    

    Explaination:

    The code above first check if you are using Enter to do confirm a code completion, if not it will indent the {|} when you type Enter. Also, it provides html tags auto indent.

    Examples:

    if( whatever ){|}
    

    press Enter and you will get

    if( whatever )
    {
      |  
    }
    

    This also works for html file. See the following example

    |
    

    press Enter and you will get

    
        |
    
    

提交回复
热议问题