我在 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 个空格的缩进。当我尝试 C-h 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 关注?
您可以将其放入您的 .emacs 文件中:
(add-hook 'python-mode-hook '(lambda ()
(setq python-indent 2)))
之所以
(setq-default python-indent 2)
不起作用可能是因为加载 .emacs 时该变量不退出。 (但我是一个 emacs 新手。我不确定我的解释。)
然而,PEP 8——Python 代码风格指南 http://www.python.org/dev/peps/pep-0008/建议“每个缩进级别 4 个空格”,我发现 4 个空格更具可读性。我实际上使用这段代码来强制缩进 4 个空格。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)