Tabify all files of project


#1

Hi,

I am using the package https://atom.io/packages/tabs-to-spaces . Is there a way we can convert all the spaces to tabs for the project?

I did tried using git settings http://stackoverflow.com/questions/2316677/can-git-automatically-switch-between-spaces-and-tabs , but some how the spaces were not converted to spaces with that technique also.


#2

You can open find-in-project (ctrl-shift-f), search for occurrences N consecutive spaces, where N is the project’s tab size, and replace them with a hard tab. To do this, you can turn on regex mode in the find-and-replace panel, put the number of spaces in the search box and put \t in the replace box


#3

Awesome dude!. I wonder why I never thought of it. Thank you.


#4

I’m sorry @olmokramer, but I think that’s too simplistic. If the tab size is 4:

    b  In this line, the first four spaces become a tab
a   b  In this line, the first three spaces become a tab
aa  b  In this line, the first two spaces become a tab

I wish it was that easy…


#5

Why would find-and-replace match 3 or 2 spaces when I search for 4 consecutive spaces?


#6

I see. I apologize, brain fart. Your idea was to replace spaces with tabs at the beginning of a line. At the beginnings of lines, it works.

Hm. But then, you are also replacing some spaces with tabs in the middle of lines. Let’s say tab width is four, and you see this (showing digits in second line to make it clearer):

aa        bb
123456789012

The above line contains eight spaces, so you replace the first four with a tab and then the second four with a tab. But actually, the first tab only advanced two characters, then the second tab advances four characters, for a total of six.

So now you have broken the visual appearance of lines that had lots of spaces in them.


#7

Converting whitespace the way people expect has a few non-obvious edge cases. You can take a look at the specs for my tabs-to-spaces package for a list.

I added an Issue for the feature here: https://github.com/lee-dohm/tabs-to-spaces/issues/33

I’d be open to a pull request for adding this feature (convert all files in a project on command). I’m not sure when I’ll be able to get to it at the moment, but it sounds like something that would be useful more than the occasional one-off.


#8

Fair enough… But when you can do your whole project in 1 go and then have to adjust for these edge cases, I think my answer was decent enough :stuck_out_tongue: Thanks for getting me acquainted with some of those edge cases, though, it’s important to keep the big picture in mind :smile: