I am very new to regular expressions, and I need some help. First I will explain my motivation and some diagrams, as it is very hard to explain otherwise.
I recently installed Vim Indent guides which displays those vertical bars/rulers like so (picture from github account):

The way it does this is by pattern matching spaces at the beginning of a line and adding them to either the IndentGuidesEven or the IndentGuidesOdd. The problem with this is that it can’t pattern match empty lines, and you get less than ideal hilighting like so:

The easiest solution is to remove all empty/blank lines, but code without spaces can be hard to read. What I had in mind is to transform the code in several stages, and ultimately adding spaces, as shown below.

What I am doing is:
- removing spaces from all lines with only spaces
%s/\s\+$//e - truncating all multiple empty lines
%s/\n\{3,}/\r\r/e - adding spaces to empty lines
%s/^\ \(\ *\)\([^\ ]\)\(.*\)\n^\ *$\n^\ /\ \1\2\3\r\ \1\r\ /gc
what that last statement does is look at three lines where the first and third non empty and the second one is empty. But this leads to problems if the first line is, for example indented in 8 times and the third one is only indented in 3 times. Is there anyway, upon finding the first pattern (in this case 8 indents) to use it in the same search pattern to make sure lines 1 and 3 start with the same number of spaces? I’m sure I could do this with an iterative function, and start from, for example 30 indents, and work my way back, but that might be a little inefficient.
I am aware that lines with only spaces are bad. However, removing spaces is trivial and I have key mapped to do this automatically. If need be I can quickly remove them. Also, I know the problem is more complex than this, and there are more cases to consider, however, I will deal with those cases as they present themselves.
Any advice as to how I could go about doing this?
In order to implement the third step according to your last comment, one can
use the following command.
This global substitution relies on the substitute with an expression feature
(see
:help sub-replace-\=) to replace empty lines with a string containinga space character repeated
min([indent(line('.')-1),indent(line('.')+1)])times. The number of spaces is calculated as a minimum of two values joined
in a temporary list. These values are indentation levels of the lines
immediately preceding and succeeding the current one (
line('.')evaluates toits number); the levels of indent are determined using the
indent()function.