Vim:按下回车键时如何缩进打开的括号或括号? [英] Vim: How to indent to an open paren or bracket when hitting enter?

查看:44
本文介绍了Vim:按下回车键时如何缩进打开的括号或括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Vim 编写 Python 已经有一段时间了,但有件事我一直无法弄清楚如何将它设置为自动缩进到最后一个打开的括号的级别.

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.

根据 pep8,如果您有一个开放式括号,并且您需要打破该行以适应 80 列,那么您应该在该开放式括号处继续下一行.示例:

According to pep8 if you have an open paren and you need to break the line to fit in 80 columns then you're supposed to continue the next line at that open paren. Example:

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

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

Obviously this is a crazy example, but that's how you're supposed to break your lines in python.

我真正想做的是设置 Vim,这样当我输入 fit,<cr> 时,它会将我的光标放在下一行的右边开放括号,所以我可以只输入 on, 等,而不是 键的某种组合事先.

What I'd really like to be able to do is set up Vim so that when I type fit,<cr> and it will place my cursor on the next line just to the right of the open paren, so I can just type on, etc. instead of some combination of <tab> and <space> keys beforehand.

我认为我永远不会相信 Vim 中 Python 代码的自动格式化程序,但如果它也有效,我会加分.

I don't think I'll ever trust the auto-formatter for python code in Vim but bonus points if that works too.

推荐答案

这可以稍微改进一下,但应该在 99% 的时间里都有效.将此添加到您的 .vimrc 中:

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\<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

这篇关于Vim:按下回车键时如何缩进打开的括号或括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆