4

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 _ Oct 21 '15 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 Oct 21 '15 at 15:22
  • You can use the python-mode-hook to set the tab-width as you want. – Nsukami _ Oct 21 '15 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 Oct 21 '15 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 Oct 21 '15 at 15:44
4

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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