Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Emacs Python mode generate TABs for indent?

Tags:

python

emacs

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.

like image 425
Aaron Watters Avatar asked Jul 13 '11 13:07

Aaron Watters


2 Answers

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)))
like image 147
jamessan Avatar answered Oct 11 '22 04:10

jamessan


probably need to do this in python mode:

(setq indent-tabs-mode t)
like image 34
jtahlborn Avatar answered Oct 11 '22 03:10

jtahlborn