在 emacs 23 中将 python 缩进设置为 2 个空格?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将其放入您的 .emacs 文件中:
不起作用的原因
可能是因为加载 .emacs 时该变量没有退出。 (但我是一个 emacs 新手。我不确定我的解释。)
但是, PEP 8 -- Python 代码风格指南 建议“每个缩进级别 4 个空格”,我发现 4 个空格更具可读性。我实际上使用这段代码来强制缩进 4 个空格。
You can put this into your .emacs file:
The reason why
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.
无论是在您的 .emacs 文件中还是在 .emacs 引用的文件中添加:
如果您想本地化,可以添加一个钩子
编辑:我发现以下问题可能会扰乱我的设置:
对于这两个问题,我发现创建挂钩和本地化变量可以有所帮助。
Either in you .emacs file or in a file referenced by your .emacs add:
You can put in a hook if you want to localize
EDIT: I have found the following the following issues can mess with my settings:
For both those issues I have found creating hooks and localizing the variables can help.
我自己刚刚遇到了这个问题,我认为 python-indent 的帮助包含了一个其他人没有提到的大线索:
如果你不自定义 python-guess-indent code> 将其设置为
nil
,然后python.el
将为每个 Python 缓冲区(包含缩进文本)自动设置python-indent
,使您的python-indent
自定义无效。 (另一方面,在罗马时......)最后,这就是我的 .emacs 文件中的内容(忽略所有其他自定义变量):
I just ran into this problem myself, and I think the help for
python-indent
contains a big clue that no one else mentioned:If you don't customize
python-guess-indent
by setting it tonil
, thenpython.el
will automatically setpython-indent
for each Python buffer (that contains indented text), making yourpython-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):