在emacs 23中将python缩进设置为2个空格? [英] Set python indent to 2 spaces in emacs 23?

查看:143
本文介绍了在emacs 23中将python缩进设置为2个空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我把以下内容放在我的.emacs中:

  ;;只有空格,没有选项卡
(setq indent-tabs-mode nil)

;;总是使用换行符
(setq require-final-newline nil)结束一个文件

;;不知道哪些可能工作
(setq-default tab-width 2)
(setq-default python-indent 2)
(setq-default py-indent-offset 2)

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

  python-indent的值是4 
缓冲区中的local_cache本地。 PY;全局值为2

如果变量
满足谓词integerp,则此变量作为文件局部变量是安全的。

文档:
Python模式中缩进单位的列数。
另请参见`M-x python-guess-indent'

您可以自定义此变量。

那就是4,不是2. Grr。我尝试自定义变量并保存,仍然4.我尝试自定义组缩进,仍然4。



我如何获得emacs注意?

解决方案

您的.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)))

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




  • 在加载库之前设置变量

  • 其他包/配置重置全局变量



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


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?

解决方案

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.

这篇关于在emacs 23中将python缩进设置为2个空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆