在 emacs 23 中将 python 缩进设置为 2 个空格?

发布于 2024-10-04 05:24:16 字数 975 浏览 5 评论 0原文

我在 Ubuntu 10.04 上使用 emacs 23.1.1。我希望在 Python 中使用 2 个空格缩进进行编程。 emacs 看起来有一个 python 的默认模式(python.el?)。

我将以下内容放入 .emacs 中:

    ;; Only spaces, no tabs
    (setq indent-tabs-mode nil)

    ;; Always end a file with a newline
    (setq require-final-newline nil)

    ;; Don't know which of these might work
    (setq-default tab-width 2)
    (setq-default python-indent 2)
    (setq-default py-indent-offset 2)

当我编辑 Python 文件时,它使用 4 个空格缩进。当我尝试 Ch v python-indent 时,它说:

    python-indent's value is 4
    Local in buffer webpage_cache.py; global value is 2

        This variable is safe as a file local variable if its value
        satisfies the predicate `integerp'.

    Documentation:
    Number of columns for a unit of indentation in Python mode.
    See also `M-x python-guess-indent'

    You can customize this variable.

也就是说,它是 4,而不是 2。我尝试自定义变量并保存,仍然4。我尝试自定义组缩进,仍然4。

如何让emacs注意?

I am using emacs 23.1.1 on Ubuntu 10.04. I wish to program in Python with a 2-space indent. emacs looks to have a default mode for python (python.el?).

I put the following in my .emacs:

    ;; Only spaces, no tabs
    (setq indent-tabs-mode nil)

    ;; Always end a file with a newline
    (setq require-final-newline nil)

    ;; Don't know which of these might work
    (setq-default tab-width 2)
    (setq-default python-indent 2)
    (setq-default py-indent-offset 2)

When I edit a Python file, it uses a 4-space indent. When I try C-h v python-indent it says:

    python-indent's value is 4
    Local in buffer webpage_cache.py; global value is 2

        This variable is safe as a file local variable if its value
        satisfies the predicate `integerp'.

    Documentation:
    Number of columns for a unit of indentation in Python mode.
    See also `M-x python-guess-indent'

    You can customize this variable.

That is, it is 4, not 2. Grr. I tried customizing the variable and saving, still 4. I tried customize-group indent, still 4.

How do I get emacs to pay attention?

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

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

发布评论

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

评论(3

兮子 2024-10-11 05:24:16

您可以将其放入您的 .emacs 文件中:

(add-hook 'python-mode-hook '(lambda () 
 (setq python-indent 2)))

不起作用的原因

    (setq-default python-indent 2)

可能是因为加载 .emacs 时该变量没有退出。 (但我是一个 emacs 新手。我不确定我的解释。)

但是, PEP 8 -- Python 代码风格指南 建议“每个缩进级别 4 个空格”,我发现 4 个空格更具可读性。我实际上使用这段代码来强制缩进 4 个空格。

You can put this into your .emacs file:

(add-hook 'python-mode-hook '(lambda () 
 (setq python-indent 2)))

The reason why

    (setq-default python-indent 2)

does not work may because this variable does not exit when .emacs is loaded. (But I am an emacs newbie. I am not sure about my explanation.)

However, PEP 8 -- Style Guide for Python Code recommends "4 spaces per indentation level" and I find 4 spaces more readable. I actually use this piece of code to force a 4 spaces indentation.

半葬歌 2024-10-11 05:24:16

无论是在您的 .emacs 文件中还是在 .emacs 引用的文件中添加:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)

如果您想本地化,可以添加一个钩子

;; Python Hook
(add-hook 'python-mode-hook
          (function (lambda ()
                      (setq indent-tabs-mode nil
                            tab-width 2))))

编辑:我发现以下问题可能会扰乱我的设置:

  • 在库之前设置变量加载
  • 其他包/配置重置全局变量

对于这两个问题,我发现创建挂钩和本地化变量可以有所帮助。

Either in you .emacs file or in a file referenced by your .emacs add:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 2)

You can put in a hook if you want to localize

;; Python Hook
(add-hook 'python-mode-hook
          (function (lambda ()
                      (setq indent-tabs-mode nil
                            tab-width 2))))

EDIT: I have found the following the following issues can mess with my settings:

  • setting the variable before the library is loaded
  • other packages/configs resetting the global variables

For both those issues I have found creating hooks and localizing the variables can help.

淡忘如思 2024-10-11 05:24:16

我自己刚刚遇到了这个问题,我认为 python-indent 的帮助包含了一个其他人没有提到的大线索:

See also `M-x python-guess-indent'

如果你不自定义 python-guess-indent code> 将其设置为 nil,然后 python.el 将为每个 Python 缓冲区(包含缩进文本)自动设置 python-indent,使您的 python-indent 自定义无效。 (另一方面,在罗马时......)

最后,这就是我的 .emacs 文件中的内容(忽略所有其他自定义变量):

(custom-set-variables
 '(python-guess-indent nil)
 '(python-indent 2))

I just ran into this problem myself, and I think the help for python-indent contains a big clue that no one else mentioned:

See also `M-x python-guess-indent'

If you don't customize python-guess-indent by setting it to nil, then python.el will automatically set python-indent for each Python buffer (that contains indented text), making your python-indent customization ineffective. (On the other hand, when in Rome...)

In the end, this is what went into my .emacs file (ignoring all other custom variables):

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