有没有办法让 == 在 vi​​m 中工作以重新缩进 python 代码?

发布于 2024-12-20 03:10:01 字数 636 浏览 1 评论 0 原文

假设我有以下 Python 代码:

if conditionOne():
    if conditionTwo():
        foo = bar
        foo += 1
        bar -= 2

如果我稍后删除 conditionTwo,我想缩进该块的三行,以便它看起来与我的所有其他代码一致。通常我只会使用 =% (我的主要语言是 C++),但这在这里不起作用,所以我在第一行尝试了 3==块。这导致了:

if conditionOne():
    foo = bar
        foo += 1
        bar -= 2

这不是我想要的。我本可以使用 3<< 并获得更好的结果,但这不是我通常使用的命令。我不想只记住 Python 的特殊缩进命令。本着别让我思考的精神,有没有办法使 = 过滤器按照我的预期与 Python 代码一起工作?

Let's say I have the following Python code:

if conditionOne():
    if conditionTwo():
        foo = bar
        foo += 1
        bar -= 2

If I later remove conditionTwo, I would like to dedent the three lines of the block so it looks consistent with all my other code. Normally I'd just reach for =% (my primary language is C++), but that won't work here, so I tried 3== on the first line of the block. That led to this:

if conditionOne():
    foo = bar
        foo += 1
        bar -= 2

That's not what I was looking for. I could have gone with 3<< and gotten a better result, but that's not a command I normally use. I'd rather not have to remember special indentation commands just for Python. In the spirit of Don't Make Me Think, is there a way to make the = filters work with Python code as I expect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-12-27 03:10:01

在 C 或 C++ 中,缩进程序不会影响其行为,但在 Python 中确实可以,因为缩进是流程控制的一部分。

因此,在Python中,具有不同缩进的程序将具有不同的行为,并且对于编辑器来说,不可能猜测开发人员是否想要缩进一行(在内部范围内)。

因此,编辑器的自动缩进功能旨在与类 C 语言一起使用,而不是与 Python 一起使用。

Whereas in C or C++ indenting a program does not affect its behaviour, in Python it could really, since indentation is part of the flow control.

Therefore in Python a program with a different indentation will have a different behaviour and for an editor it is impossible to guess whether the developer wanted to indent a line (in an inner scope) or not.

Hence auto-indenting features of your editor are designed to work with C-like languages, not Python.

苍白女子 2024-12-27 03:10:01

如果您使用 vim-indent-object 插件,您可以执行以下操作删除行并缩进块:

  • 将光标放在条件上:
  • 将光标放在块中的任意位置:

考虑到这一点,也许你可以:nmap =% :nmap == 并根据需要删除条件。这不是一个完美的解决方案,但对我来说似乎是一个不错的选择。

If you use the vim-indent-object plugin, you could do the following to delete the line and dedent the block:

  • With the cursor on the conditional: <iidd
  • With the cursor anywhere in the block: <aidd

With this in mind, maybe you could :nmap =% <ii and :nmap == <ai and delete the conditional as you wish. This isn't a perfect solution but it seems like a decent alternative to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文