Search code examples
regexreplaceindentation

How To Remove Indents From Code?


Lets consider this example of code... Don't look at code, but at indents.

        protected function _hashPassword( $password, $salt, $nuts = '' ) {

            if ( $nuts === '' ) {
                $nuts = Kohana::config( 'a11n' )->nuts;
            }


            $password =
                sha1(
                    $password
                    . $salt
                    . $nuts
                );


            return $password;

        }

It's taken from much bigger source code. As you can see, it's indented by 2 tabs. I want to somehow remove indent from it without using typing. Somehow.

If I use in-editor build-in 'Replace' function and remove those two tabs like...

'Replace' function

I get something like this (not in all cases, but almost)...

protected function _hashPassword( $password, $salt, $nuts = '' ) {

    if ( $nuts === '' ) {
$nuts = Kohana::config( 'a11n' )->nuts;
    }


    $password =
sha1(
    $password
    . $salt
    . $nuts
);


    return $password;

}

It's because there are more than only two tabs on one line and it replaces all 4 tabs.

I'm looking for regular expression that's powerful enough to remove indent nicely! Maybe there are any other solutions? Just don't suggest to write code without indents!


Solution

  • Select the code in your favorite modern editor, and hit Shift+Tab.