Search code examples
regexgrepbbedit

Regular Expression newline \r and \t based on previous line?


I'm trying to manipulate lines in BBEdit but I'm having issues with the indention. What I am trying to do is take a line such as this:

Lorem Ipsum Foobar the summer..

locate Foobar and do a \r \t so it will look like this:

Lorem Ipsum 
   Foobar the summer..

but based on the previous line in the file if Lorem Ipsum is indented the file will result into this:

     Lorem Ipsum
Foobar the summer..

I know how to find the indention and return it in BBEdit but if I'm running this globally my indention may be off because some areas in the file may be indented differently. So my question is how can I write my scope to return and indent based on the previous's line so it may be (previous indention + \t)?


Solution

  • You could capture the indentation and use it in the replace?

    In regex, I would use something like this (raw string):

    ^(\s*)(.*?)(?=Foobar)
    

    With a replace of $1$2\r$1\t.

    $1 contains the initial indentation, and you add a new one to it with $1\t.

    regex101 demo