I recently installed spacemacs with the python mode and would like to change the default indentation to be TAB instead of four spaces.
What I did is I added (setq-default indent-tabs-mode t tab-width 4)
to my just after(setq initial-major-mode 'text-mode)
in my config.el
. Unfortunately, when I open a python file, I can see that indentation is still done with 4 spaces instead of one TAB. Did any of you come across this issue before ?
Thanks in advance
1 Answer
Anaconda-mode is a minor-mode which has nothing to do with indentation AFAIK. So I assume that you actually mean python-mode
.
The second line in python-mode
is
(set (make-local-variable 'indent-tabs-mode) nil)
which overrides the default value.
So you must set indent-tabs-mode
in python-mode-hook
:
(add-hook 'python-mode-hook (lambda () (setq indent-tabs-mode t)))
Using spacemacs
you can add that line to the function dotspacemacs/user-config
in your spacemacs
initialization file -- most likely ~/.spacemacs.d/init.el
or ~/.spacemacs
.
-
And the previous line similarly sets
tab-width
to 8.python-mode
purposefully makes it difficult, because Python's layout rules mean that it's easy to shoot oneself in the foot. E.g. the Python language defines TABs as occupying 8 columns, so using atab-width
different from 8 means that the file will not be displayed in the way that python will interpret it. It may still work, of course, but: buyer beware.– StefanJun 4, 2018 at 16:02 -
1@Stefan AFAIK it is no problem if one uses consistently tabs for indentation. Real problems occur if one mixes tabs and spaces.– TobiasJun 4, 2018 at 16:08
-
@Stefan I tended to
(define-minor-mode indent-tabs-mode "Toggle indent-tabs-mode.") (add-hook 'python-mode-hook #'indent-tabs-mode)
. Is there any reason whyindent-tabs-mode
is not defined as a real minor-mode? (If you want I can also ask this as a proper question here on emacs.sx.)– TobiasJun 4, 2018 at 16:13 -
Thanks for your answer, I am really new on spacemacs could you advise me in which of the function (
python/init-python
maybe?) I should paste the code you prioovided in your answer Jun 4, 2018 at 17:02 -
@RobinNicole I added some text about the installation of the hook in your init-file at the end of the answer.– TobiasJun 4, 2018 at 20:55