Help with Tabs


#1

Hello everyone,
I have recently decided to change my code formatting style from indenting every line 1 tab between the margin to being right up against it. This would take weeks to accomplish. I apologize for the lack for explanation, however I have attached an example of what I am trying to accomplish. Is it possible to turn this:

    class myClass {
         public function greet() {
             echo 'Hello world!';
         }
    }

Into this:

class myClass {
  public function greet() {
        echo 'Hello world!';
   }
}

I know the example is using spaces, but this forum does not support tabs. I need to do this across my entire project.

Thank you in advance and if you need me to clarify anything, let me know.


#2

If you want to reduce an entire selection exactly one indentation level, you can select the entire contents of the file with Cmd+A on OS X or Ctrl+A on other platforms and then press Shift+Tab.


#3

You can use the find-and-replace package (it’s a core package, comes with Atom) to accomplish this.

  1. Make a backup of your project in case the replacement has unwanted side-effects.
  2. Open your project.
  3. Enter project-find:show into the Command Palette (Ctrl+Shift+F on Windows).
  4. Make sure the RegEx option is enabled by clicking on the [.*] button if it’s not.
  5. Enter ^\t into the search field. This searches for lines starting with a tab.
  6. Leave the replacement field empty and hit ‘Replace All’. All tabs at the start of each line in each file in the project should be removed.

Once again, make sure to make a backup first. It works fine on a simple test case like in the example you provided, but perhaps there are edge-cases or files with mixed tabs/spaces where the replacement does something ugly.


#4

This worked great, however it took a little bit longer than I wanted it to. Thanks for your contribution.


#5

I toggled find in my project and only found tabs in my dependencies (the file/directory pattern was empty). I am guessing that maybe the tab size is different in my project. Any other ideas? Thank you.


#6

You can find out what is being used in your files by enabling Show Invisibles in the Settings:

The symbols for each invisible character (tab, space, etc) is shown in the settings. The result in the editor will be something like this:

In the example above, each line starts with a single tab.

If for example your lines start with two tabs, change the regular expression in step 5, to ^\t\t. If your lines start with 4 spaces, change the regular expression to ^ followed by 4 spaces.