indent-tabs-mode

Registered by Andreas Roehler

Source: http://stackoverflow.com/questions/6679625/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. 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.
python emacs
link|edit

asked Jul 13 '11 at 13:23
Aaron Watters
39% accept rate

2

Don't worry, there is a special circle of hell for this sort of individuals, too. – Evpok Jul 13 '11 at 13:33

Stop working with those guys! ;-)) – Johannes Charra Jul 13 '11 at 13:34
1

Since when was "tab" an acronym? – BoltClock♦ Jul 13 '11 at 13:35
1

Please do "cause trouble" by removing the real trouble. Using tabs is a Bad Idea. – Wooble Jul 13 '11 at 13:50

TABs in source code are a bad idea. So bad we've a commit hook that blocks a commit when one of the files contains TAB unless it's a binary or is named "Makefile" or there is a special secret password in the commit comment. – 6502 Jul 13 '11 at 14:00
show 2 more comments
feedback
3 Answers
active oldest votes
up vote 2 down vote accepted

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

link|edit

edited Jul 18 '11 at 17:11

answered Jul 13 '11 at 13:52
jamessan
8,591921

I tried putting this in various places and can't make it work. My emacs installation seems to be using "python.el.gz" which I thought about hacking. Should I get python-mode.el instead? – Aaron Watters Jul 18 '11 at 15:46

@Aaron: You probably did get it to work, but since python.el only indents 4 columns per level by default you weren't seeing the use of tabs yet. I updated the answer with some more explanation. – jamessan Jul 18 '11 at 17:13
feedback
up vote 0 down vote

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

edited Jul 13 '11 at 14:29

answered Jul 13 '11 at 14:10
Gareth Rees
12k1541

personally, i dislike file local variables, especially if you are collaborating. imagine what the file would look like if each user used a different editor, and they all saw fit to include different blurbs for their personal editor. blech. – jtahlborn Jul 13 '11 at 17:14

BUT formatting settings are right to place to file!! Do you vote for style breaking? – gavenkoa Oct 1 '11 at 19:17
feedback
up vote 0 down vote

probably need to do this in python mode:

(setq indent-tabs-mode t)

Blueprint information

Status:
Complete
Approver:
None
Priority:
Medium
Drafter:
None
Direction:
Needs approval
Assignee:
Andreas Roehler
Definition:
Drafting
Series goal:
None
Implementation:
Implemented
Milestone target:
None
Started by
Andreas Roehler
Completed by
Andreas Roehler

Related branches

Sprints

Whiteboard

instead of a setting as done now:

  (when (/= tab-width py-indent-offset)
    (setq indent-tabs-mode nil))

python-mode should set tab-with
tab-with is a local variable, so no harm should come from

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.