简体   繁体   English

如何使 Emacs Python 模式为缩进生成 TAB?

[英]How to make Emacs Python mode generate TABs for indent?

I'm working with a bunch of Python programmers who use vim and they make Python using TABs for indent.我正在与一群使用 vim 的 Python 程序员合作,他们使用制表符制作 Python 用于缩进。 I use Emacs with python-mode which translates the tab key to 4 spaces (like it should, but never mind).我将 Emacs 与 python-mode 一起使用,它将制表键转换为 4 个空格(应该如此,但没关系)。 Since I don't want to cause trouble I want to add something to my.emacs file (or whatever) to make indents using real TABS instead of translating them to spaces.因为我不想造成麻烦,所以我想在 my.emacs 文件(或其他文件)中添加一些内容,以使用真正的 TABS 进行缩进,而不是将它们转换为空格。 How?如何?

I'm sorry if this is answered somewhere else: I didn't find it.如果这在其他地方得到回答,我很抱歉:我没有找到它。

You can define Python-specific settings in your ~/.emacs with python-mode-hook .您可以使用python-mode-hook~/.emacs中定义 Python 特定的设置。 In order to use tabs for indentation, you could use:为了使用制表符进行缩进,您可以使用:

(add-hook 'python-mode-hook
  (lambda () (setq indent-tabs-mode t)))

Since python.el indents only 4 columns, by default, the above will use tabs when the indent is a multiple of 8 and tabs followed by spaces for other indents.由于python.el仅缩进 4 列,默认情况下,当缩进为 8 的倍数时,上面将使用制表符,其他缩进时使用制表符后跟空格。

If you need to use a single tab for every indent level, you'll also need to set python-indent to 8. Then you can set tab-width to whatever width you want to see the tabs displayed as.如果您需要为每个缩进级别使用一个标签,您还需要将python-indent设置为 8。然后您可以将tab-width设置为您希望看到标签显示为的任何宽度。

(add-hook 'python-mode-hook
  (lambda ()
    (setq indent-tabs-mode t)
    (setq python-indent 8)
    (setq tab-width 4)))

probably need to do this in python mode:可能需要在 python 模式下执行此操作:

(setq indent-tabs-mode t)

As the commenters to the post correctly said, using tabs for indentation is a bad idea, and using a non-standard tab width is even worse.正如帖子的评论者正确所说,使用制表符进行缩进是一个坏主意,使用非标准制表符宽度甚至更糟。 Nonetheless, sometimes you have no choice if you want to collaborate.尽管如此,如果你想合作,有时你别无选择。

Depending on exactly how your colleagues have vim configured, you may need to both turn on indent-tabs-mode and set tab-width to 4.根据您的同事如何配置 vim,您可能需要同时打开indent-tabs-mode并将tab-width设置为 4。

A convenient way to do this, that won't mess up your other work, is to use file local variables .一种不会弄乱您的其他工作的便捷方法是使用文件局部变量 At the end of each offending file, put this:在每个有问题的文件的末尾,放上:

# Local Variables:
# indent-tabs-mode: 1
# tab-width: 4
# End:

(You'll have to tell Emacs that indent-tabs-mode is a safe local variable.) (你必须告诉 Emacs indent-tabs-mode是一个安全的局部变量。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM