Vim:按下回车键时如何缩进打开的括号或括号?

编程入门 行业动态 更新时间:2024-10-27 09:42:34
本文介绍了Vim:按下回车键时如何缩进打开的括号或括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 18:17:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   回车键   按下   Vim

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!