3

I'm trying to mass indent my code in the way I prefer it - that is, each line should be indented by groups of 4 spaces, depending on how 'deep' that line is in code (e.g. children elements should get 4 extra than their parent).

Currently everything has 1 space (a sample from my code):

<html>
 <head>
  <title>Test</title>
  <link rel="stylesheet" href="style.css">
  <script src="jquery.js"></script>
  <script src="loadfiles.js"></script>
 </head>
...

I'd like to have it get 4 spaces for the first level, 8 for the second etc. So basically multiply the amount with 4.

I tried this Regex replace command:

^ (.*)$      // search for
    $1       // replace with

But this only replaces the first space of each line with 4 spaces. How can I also make it replace 2 spaces with 8 spaces etc.?

Thanks.

1
  • 1
    enable the global flag in your RegEx Commented Mar 4, 2011 at 19:47

3 Answers 3

10

Try this:

^(\s+)  //search for
$1$1$1$1 //replace with
Sign up to request clarification or add additional context in comments.

Comments

1

I think Cybernate is right. Just thought I'd mention you could accomplish this without any code by using block edit (column edit) feature in your favorite text editor (such as Notepad++, Coda etc.)

Comments

1

Assuming you are on a system with perl, you could do this:

cat original.html | perl -lpe 's/^( +)/" "x(length($1) * 4)/e' > indented.html

That is, replace spaces in the beginning of a row by four times as many spaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.