开发者

How to make Emacs Python mode generate TABs for indent?

I'm working with a bunch of Pyt开发者_运维百科hon programmers who use vim and they make Python using TABs for indent. I use Emacs with python-mode which translates the tab key to 4 spaces (like it should, but never mind). 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. 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. 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.

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.

(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:

(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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜