0

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

0

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 a tab-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. – Stefan Jun 4 '18 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. – Tobias Jun 4 '18 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 why indent-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.) – Tobias Jun 4 '18 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 – Robin Nicole Jun 4 '18 at 17:02
  • @RobinNicole I added some text about the installation of the hook in your init-file at the end of the answer. – Tobias Jun 4 '18 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.