18

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.

0

3 Answers 3

19

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)))
Sign up to request clarification or add additional context in comments.

2 Comments

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: 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.
3

probably need to do this in python mode:

(setq indent-tabs-mode t)

Comments

0

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

2 Comments

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.
BUT formatting settings are right to place to file!! Do you vote for style breaking?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.