Vim:打击进入时如何缩进打开的支架或支架?

时间:2012-02-29 01:24:52

标签: python vim pep8 auto-indent

我一直用Vim编写Python一段时间,但有一点我无法弄清楚如何做到这一点将它设置为自动缩进到最后一个打开的paren的级别。

根据pep8,如果你有一个开放的paren并且你需要打破线以适应80列,那么你应该继续在那个开放的paren的下一行。例如:

calling_some_really_long_function(that, has, way, too, many, arguments, to, fit,
                                  on, one, line)

显然这是一个疯狂的例子,但这就是你应该如何在python中打破你的界限。

我真正希望能够做的是设置Vim,这样当我输入fit,<cr>时它会将光标放在开放式按钮右侧的下一行,所以我可以只需输入on,等,而不是预先<tab><space>键的某种组合。

我认为我永远不会相信Vim中的自动格式化程序用于python代码,但如果有效的话,我会相信奖励积分。

3 个答案:

答案 0 :(得分:1)

这可以稍微改进一下,但应该在99%的时间内工作。在.vimrc中添加:

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

au FileType python inoremap <Cr> <Esc>:call PythonEnterFunc()<CR>a

答案 1 :(得分:0)

使用gq,使用VISUAL块进行整个选择,或使用gqqgqj

等动作

答案 2 :(得分:0)

至少从V7.0开始包含在Vim中:

请参阅usr/share/vim/vim80/indent/python.vim(第74行)https://github.com/vim/vim/blob/master/runtime/indent/python.vim

中的以下摘录
function GetPythonIndent(lnum)
  ...
  " When inside parenthesis: If at the first line below the parenthesis add
  " two 'shiftwidth', otherwise same as previous line.
  " i = (a
  "       + b
  "       + c)
  call cursor(a:lnum, 1)
  let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
      \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
      \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
      \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
  if p > 0
    if p == plnum
      " When the start is inside parenthesis, only indent one 'shiftwidth'.
      let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
      \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
      \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
      \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
      if pp > 0
    return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
      endif
      return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
    endif
    if plnumstart == p
      return indent(plnum)
    endif
    return plindent
  endif
  ...