1

I have a bunch of python files (ie .py files), which I use Emacs to edit. However I keep having the issue where sometimes emacs will indent regions with 8 spaces instead of 4, and I can't figure out why.

For example, sometimes when I define a function and hit return, Emacs will indent the following code by 8 spaces instead of the usual 4, and pressing TAB will only allow me to cycle between 0 indent and 8 spaces. If I manually enter 4 spaces for the first line, then following ones will appear at the same 4-space indentation, but if I then want to say put in a for loop, I will have the same issue again with the following code being indented by 8 spaces relative to the for ...: command.

I can't seem to sort this out by changing anything in my .emacs file (though I have only been using Emacs for a few months so maybe I'm doing something wrong!) - I have tried putting in

(setq tab-width 4)  

and

(setq-default indent-tabs-mode nil)  
(setq-default tab-width 4)  
(setq indent-line-function #'insert-tab)  

which I found from answers to other people's questions, and neither of them have any impact. It's also an intermittent problem, it doesn't seem to happen for every file.

Hope someone can offer some help! And I apologise if this is a duplicate question, I searched around but couldn't find anyone else with the exact same issue so I don't think it is!

0

2 Answers 2

2

I assume you always want an indentation with 4 spaces even if there are indents of width 8 lurking around in your python file.

Set python-indent-guess-indent-offset to nil and keep the default value 4 for python-indent-offset.

1
  • Thank you @Tobias, I think this has fixed it!
    – Bethan
    Commented Feb 28, 2020 at 14:10
1

When taking other people's code, it's important to understand what it does.
For that you can use C-h o (aka describe symbol). E.g. C-h o indent-tabs-mode RET will tell you:

Indentation can insert tabs if this is non-nil.

and C-h o tab-width RET:

Distance between tab stops (for display of tab characters), in columns. NOTE: This controls the display width of a TAB character, and not the size of an indentation step. This should be an integer greater than zero.

So those won't help fix your problem.
Indentation rules and styles are different for each language, so you need to look for a setting that's specific to python-mode. In your case, I think Tobias is right that the most likely problem comes from python-indent-guess-indent-offset. Not sure how you could have found it, other than by doing something like M-x customize-group RET python RET which lets you see (and change) the customization variables of python-mode.

2
  • This is an example for a good negative answer. It explains how you could have discovered yourself why you were wrong. I don't know the conventions with negative answers here on SE. But I like it +1.
    – Tobias
    Commented Feb 28, 2020 at 14:02
  • Thank you Stefan, that's really helpful. I did find it easier using the M-x customize-group way rather than editing the .emacs file myself.
    – Bethan
    Commented Feb 28, 2020 at 14:12

Your Answer

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

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