Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to convert python indentation from spaces to tabs? [closed]

I have a some .py files that use spaces for indentation, and I'd like to convert them to tabs.

I could easily hack together something using regexes, but I can think of several edge cases where this approach could fail. Is there a tool that does this by parsing the file and determining the indentation level the same way the python interpreter does?

like image 291
Corey Avatar asked Dec 03 '08 21:12

Corey


3 Answers

If there are not many files to convert, you can open them in vim, and use the :retab command.

See the vim documentation for more information.

like image 148
wimh Avatar answered Oct 12 '22 21:10

wimh


Python includes a script for the opposite (tabs to spaces). It's C:\Python24\Tools\Scripts\reindent.py for me

like image 43
Greg Avatar answered Oct 12 '22 21:10

Greg


:retab will swap tab with spaces, and :retab! will swap spaces with tab. 1 tab = 4 spaces, 4 spaces = 1 tab, depending on your tab setting.

like image 21
Orjanp Avatar answered Oct 12 '22 19:10

Orjanp