Vim Auto Indent with newline

前端 未结 6 1116
礼貌的吻别
礼貌的吻别 2021-02-01 13:18

How do I get vim to place the cursor within the braces starting on a new line, ie with | denoting the cursor position :

class {
  |
}

right now

6条回答
  •  遇见更好的自我
    2021-02-01 14:16

    I wrote this in my .vimrc

    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
    

    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.

    For your problem:

    class {|}
    

    press Enter and you will get

    class {
        |
    }
    
    |
    

    press Enter and you will get

    
        |
    
    

提交回复
热议问题