Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set Emacs tab size to 4 chars wide for .py files?

Tags:

emacs

I've tried using the following:

(setq-default tab-width 4) (setq-default tab-stop-list (list 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60)) 

But the size of tabs when editing .py files is still 8 chars wide. In other files it has gone down to 4, so I assume the Python major mode is overriding this somehow. I see that I can set python-indent to 4, but this causes spaces to be inserted (which goes against our code style guide).

How do I make the tabs 4 chars in width?

Update:

I've also tried this, but it didn't do anything:

(add-hook 'python-mode-hook   (setq indent-tabs-mode t)   (setq tab-width 4) ) 
like image 723
Nick Bolton Avatar asked Sep 10 '10 12:09

Nick Bolton


People also ask

How do I change the width of a tab in Emacs?

With tab-width equal to the default value of 8, Emacs would insert 1 tab plus 2 spaces. Use t rather than nil to tell Emacs to use tab characters where appropriate. If you only want this in a particular mode, add (setq indent-tabs-mode nil) to that mode's hook.

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.

How big is a tab in python?

By default the tab size is 4 chars. The python 2 interpreter itself expands tabs to 8 chars, not 4. The Linux terminal ('cat' a python source file) and Windows (type a python source file) also expand tabs to 8 spaces.


1 Answers

(add-hook 'python-mode-hook       (lambda ()         (setq indent-tabs-mode t)         (setq tab-width 4)         (setq python-indent-offset 4))) 
like image 62
Tao Peng Avatar answered Oct 07 '22 01:10

Tao Peng