When I write Python code, I personally prefer indenting with 4 spaces instead of a tab. However, in my day job I work with a codebase that uses only tabs.

When I open one of these files in emacs, tabs display eight spaces wide. I have tried adding both (setq-default tab-width 4) and (setq tab-width 4) to my init file, but neither of these seem to have any affect on this problem. I have used whitespace-mode to confirm that it is indeed one tab that I am seeing and not two, and that they are indeed eight spaces wide. When I check the value of the variable tab-width from whatever buffer I am having the problem in, I get the following message:

tab-width is a variable defined in `C source code'.
Its value is 8
Local in buffer <whateverbuffer>; global value is 4

I know that I want Its value to be 4 instead of eight, but I have not had any luck setting it. Also, in the documentation it says of tab-width: Automatically becomes buffer-local when set...I have a feeling my problem stems from this, but I don't quite understand what this means. Any suggestions? Thank you!

  • 1
    Are you editing those python files using the python mode?
    – Nsukami _
    Commented Oct 21, 2015 at 15:18
  • Yes, I am. And I just noticed that when I change into another mode, the tabs display as I want them too. I am assuming I need to specify in my init for python-mode to use a tab-width of 8, but not sure how...
    – elethan
    Commented Oct 21, 2015 at 15:22
  • You can use the python-mode-hook to set the tab-width as you want.
    – Nsukami _
    Commented Oct 21, 2015 at 15:37
  • 1
    You may need to set python-indent-offset also, which should be done outside of the major-mode hook so its customized value is used when python-mode first initializes. If @Nsukami_ could take a look at my draft answer (deleted) and post a proper answer, that would be appreciated. I'm still not familiar enough with python-mode and I don't want to steal @Nsukami_'s thunder.
    – lawlist
    Commented Oct 21, 2015 at 15:41
  • Thank you, that did it! Actually, buried in my init file I already set python-indent-offset appropriately, and set tab-width using the python-mode-hook - my problem was that I was using setq-default instead of setq. I will research the difference between these two to better understand why it wasn't working the way I had it.
    – elethan
    Commented Oct 21, 2015 at 15:44

1 Answer 1


The first line when enabling python-mode is (set (make-local-variable 'tab-width) 8). Because tab-width is a buffer-local variable, a new value would need to be set subsequent to the code above. The most common way this is done is with a major-mode hook, in this case the python-mode-hook.

There is also a variable called python-indent-offset, which is used when the function python-indent-guess-indent-offset can't figure out what indentation to use. That should be set outside of the major-mode hook so that its custom value is available when python-mode first initializes.

(setq python-indent-offset 4)

(defun python-custom-settings ()
  (setq tab-width 4))

(add-hook 'python-mode-hook 'python-custom-settings)

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.