Indentation for indentation

I am trying to format my code in the way I prefer, i.e. each line should be indented in groups of 4 spaces, depending on how “deep” this line is in the code (for example, children should get 4, except for their parent).

Currently, everything has 1 space (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 would like him to have 4 places for the first level, 8 for the second, etc. So basically multiply the sum by 4.

I tried the Regex replace command:

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

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

Thank.

+3
source share
3 answers

Try the following:

^(\s+)  //search for
$1$1$1$1 //replace with
+9

, . , , - , ( ) (, Notepad ++, Coda ..).

+1

Assuming you are on a perl system, you can do this:

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

That is, replace the spaces at the beginning of the line with four times as much.

+1
source

Source: https://habr.com/ru/post/1796377/


All Articles