Vim: How to indent to an open paren or bracket when hitting enter?

后端 未结 3 927
终归单人心
终归单人心 2020-12-31 09:29

I\'ve been programming Python with Vim for a while but one thing I haven\'t been able to figure out how to do it set it to auto indent to the level of the last open paren.

3条回答
  •  执笔经年
    2020-12-31 10:04

    This can be refined a bit, but should work 99% of the time. Add this in your .vimrc:

    function! PythonEnterFunc()
      let l:li = getline('.')
      execute "normal! a\"
      if l:li =~ '([^)]*$'
        let l:pos = stridx(l:li, '(') + 1
        for i in range(l:pos)
          execute "normal! a\"
        endfor
      endif
    endfunction
    
    au FileType python inoremap  :call PythonEnterFunc()a
    

提交回复
热议问题