首页 > 解决方案 > Emacs 和 python 模式自动缩进

问题描述

python 模式自动缩进似乎基于样式做出了很多假设。在某些情况下,如何配置缩进方式,或者使其像 vim 一样缩进。

例如,如果我有以下功能:

def some_function(
    arg1,
    arg2,
):
    pass

如果我走到arg2行尾,按回车键,然后输入arg3,,这就是我将得到的:

def some_function(
    arg1,
    arg2,
        arg3,
):
    pass

我想要的是:

def some_function(
    arg1,
    arg2,
    arg3,
):
    pass

我怎样才能让它在这里做我想做的事?

我已经尝试过(setq indent-line-function 'indent-relative),哪种是我想要的,除非我按两次回车,光标将位于行的开头(似乎它只检查最后一行以确定相对性)。

在 vim 中,我可以输入def some_function(并按 enter,它会自动缩进 4 个空格。如果我输入两次 enter ,它仍然会缩进 4 个空格。这是我最想要的。

提前致谢!

标签: emacsspacemacs

解决方案


看起来这解决了我遇到的自动缩进问题。这不是让 emacs 使用 vim 样式缩进的方法。如果我弄清楚了,我会把它添加到这个答案中。

(add-hook 'python-mode-hook
          (lambda ()
            (setq python-indent-def-block-scale 1)))

推荐阅读