Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert txt file with mixed spaces/tabs to tabs only (where possible) [closed]

I have some source code file which has mixed tabs/spaces and I want to convert it to a file where it has automatically replaced all indentation spaces by tabs for a given tab space length (i.e. for example tab = 2 spaces).

Any easy solution (with common Unix tools, MacOSX, bash or zsh)? Some sed script or Python command or so?

Thanks, Albert

like image 411
Albert Avatar asked Jul 18 '10 17:07

Albert


People also ask

How do you fix mixed tabs and spaces?

Go into its settings and turn off "Fix Mixed Tabs"... If you have the default VS settings, tabs in the editor are converted to spaces. So (in theory) if you clicked Untabify when the message comes up, it should convert all tabs to spaces and the errors should not appear next time.

How do I change tabs to spaces in Linux?

To convert tabs in a text file to spaces, you can use the expand command. To convert each tab to a single space character, use the -t 1 option.


1 Answers

Ok, none of the given solutions satisfied me, so I coded it myself. :)

See here:

  • http://github.com/albertz/helpers/blob/master/indent-spacestotabs.py
  • http://github.com/albertz/helpers/blob/master/indent-tabtospaces.py
like image 148
Albert Avatar answered Dec 09 '22 17:12

Albert