Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-indent Python code after changing indent width in Emacs?

Tags:

python

emacs

I changed python-indent from 3 to 4. I then mark-whole-buffer and indent-for-tab-command. It gave me garbage.

like image 856
an0 Avatar asked Jan 10 '13 21:01

an0


2 Answers

There is the indent-region function. So I'd try mark the whole buffer, then M-x and type indent-region. It's usually bound to C-M-\, as far as I know.

Edit

Re-indentation does not work for a tab-width change. As I wrote in the comments changing spaces to tabs and then altering the tab-width is a solution:

"Guessing you are indenting with space and not tabs, you'd first do tabify on the buffer content with your tab-width set to 3. Then change tab-width to 4 and run untabify."

like image 142
Bernhard Avatar answered Sep 22 '22 00:09

Bernhard


This is kind of a hack, but it won't give you the garbage that indent-region is giving you

1) Make sure tabs as spaces are set to 4 spaces. In a scratch buffer type:

(setq tab-width 4)

And then evaluate it by marking it and using M-x eval-region

2) Globally replace all sets of three spaces with a tab character

M-x replace-regexp [SPC][SPC][SPC][RET] C-q[TAB][RET]

3) Highlight the whole buffer and untabify

M-x mark-whole-buffer M-x untabify

This will convert all tabs into four spaces.

like image 29
Wilduck Avatar answered Sep 20 '22 00:09

Wilduck