Python indentation in Spacemacs with hard tabs is

2019-07-28 21:47发布

问题:

Let me preface by saying that I am a relatively new emacs/spacemacs convert from vim, so my my knowledge is still pretty basic.

I have spacemacs set up with the python layer, with the additional package of dtrt-indent listed in dotspacemacs-additional-packages. And setting

(dtrt-indent-mode t)

in the dotspacemacs/user-config. I need the dtrt-indent as I am working on a few projects (in python and lua) where I am not the one who sets the indentation rules.

This configuration works fine for python files with soft-tabs of varying length. This configuration also works for lua files that have hard tabs.

When I open a python file indented with hard tabs something seems to get messed up, it appears to think that the indentation is two hard tabs. Meaning, if I have code like this (pretend that the 4 spaces are tabs):

def func():|  <--- Cursor
    print 'line'

and press enter, I get:

def func():
        |  <--- Cursor
    print 'line'

The status line prints dtrt-indent's message saying:

Note: indent-tabs-mode adjusted to t

Note: As stated I'm pretty new at this, and my spacemacs configuration is pretty sparse. There is nothing else in my user-config, I have not custom layers, I don't even have any additional packages other than dtrt-indent.

EDIT:

python-indent-offset is set to 8

indent-tabs-mode is set to t

tab-width is set to 4

回答1:

Try M-x whitespace-mode and repeat this experiment. What do you see? Did you indent with spaces or with tabs?

I'm guessing you'll see something like this:

def·func():
········
»   print·'line'

This would mean you have python-indent-offset set to 8, indent-tabs-mode set to nil (aka indent with spaces), and tab-width set to 4.

If that's not the case, what are the values of those variables (C-h v python-indent-offset, etc.)?

If that is the case, you can fix the problem by running

(setq-default python-indent-offset 4)