Regex to change the number of spaces in an indent level

后端 未结 4 2064
天命终不由人
天命终不由人 2021-02-12 23:54

Let\'s say you have some lines that look like this

1  int some_function() {
2    int x = 3;  // Some silly comment

And so on. The indentation i

4条回答
  •  猫巷女王i
    2021-02-13 00:18

    I needed to halve the amount of spaces on indentation. That is, if indentation was 4 spaces, I needed to change it to 2 spaces. I couldn't come up with a regex. But, thankfully, someone else did:

    //search for
    ^( +)\1 
    //replace with (or \1, in some programs, like geany)
    $1 
    

    From source: "^( +)\1 means "any nonzero-length sequence of spaces at the start of the line, followed by the same sequence of spaces. The \1 in the pattern, and the $1 in the replacement, are both back-references to the initial sequence of spaces. Result: indentation halved."

提交回复
热议问题