I am replacing all space indentations in my python files with tab indentations, because for some reasons my indent tool doesn't make the replacement (it does for js, html, css files though).
I am trying to globally replace the pattern with vscode using a regexp, but I can't find the right expression to use. I thought about replacing all four-spaces with tabs: / /g
, however at some places in the code there are some special indentation in the middle on an expression that I want to keep. They intend to make the code more readable, and look like this:
a = 1
aaaaaaa = 2
aaaa = 3
Replacing these four-spaces will break the alignment, so I want to keep these space indentations (I might replace them later but for now I don't need to bother doing this).
The new pattern I found is a newline followed by an unknown amount of four-spaces: /\n( )*/g
. This searches for all classic indentations, excluding the very specific indentations in the middle of an expression. However I cannot find a way to replace these patterns with the correct amount of tabs: I am not replacing one four-space, I am replacing an unknown amount of four-spaces.
How can I replace an unknown amount of repetitions of a pattern with a regexp?
You can use
(?<=^(?: {4})*) {4}
Replace with \t
. See the regex demo.
Screenshots with the results:
Details:
(?<=^(?: {4})*)
- a positive lookbehind that matches a location that is immediately preceded with start of a line and then any amount of four space strings {4}
- four spaces.