Can Textmate Sort My Code into Readable Columns?

I like to indent repetitive lines of code so they are easy to look at and see small differences. As an example, this is hard to read:

address = "1800 Washington St."
name = "George McGoo"
user_type = "admin"

but this is easy to read:

address   = "1800 Washington St."  
name      = "George McGoo"  
user_type = "admin"

Is there a way to do this in Textmate without wearing out my space bar?

1 Answer

I don't know anything about textmate, but you can solve the issue at hand with the command-line:

$ cat addresslist 
address = "1800 Washington St."
name = "George McGoo"
user_type = "admin"

address = "1800 Washington St."
name = "George McGoo"
user_type = "admin"

$ awk -F'=' '{a[NR]=$0;l=length($1);if(l>max){max=l};last=NR}END{for(i=1;i<=last;i++){if(a[i]~/=/){c=a[i];gsub(/^[^=]+*=/,"",c);b=a[i];gsub(/=.*$/,"",b);printf "%-*s = %s\n",max,b,c}else{print a[i]}}}' addresslist
address    =  "1800 Washington St."
name       =  "George McGoo"
user_type  =  "admin"

address    =  "1800 Washington St."
name       =  "George McGoo"
user_type  =  "admin"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest