问题
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...

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!
回答1:
select the code in your favorite modern editor, and hit shift-tab
回答2:
Surely you want to simply replace ^\t\t
, or use whatever character your editor has for the start-of-line anchor if it's not ^
(I have no idea what the capabilities of your particular editor are, only a (pretty good) understanding of how most regular expression engines are driven).
This will only replace two-tab items at the start of each line.
For example, if I were doing this from the command line under Linux,
sed 's/^\t\t//g' oldfile.c >newfile.c
would do what you desire.
回答3:
Using regular expressions, replace ^\t\t
with the empty string.
Without using regular expressions, simply replace \n\t\t
with \n
.
Also consider using a source code formatter.
回答4:
The following applies to Notepad++:
1) Set search mode to: Regular expression
2) Find what: ^[\t]+
3) Replace with:
Edit: This will replace 1 or more tabs from the beginning of every line.
Alternative method: select the whole source code and hit Shift-Tab
multiple times. It will remove the tabs from the beginning of every line, just like the regex above. I hope this helps!
来源:https://stackoverflow.com/questions/6454466/how-to-remove-indents-from-code