Your global command is explained in details here.
g/^\s*$/,/./-j
If I understood the post correctly, I think your problem comes from this part:
,/./-
It defines the range of lines on which the join
command has to operate.
The range begins with the current line (an empty line which can contain whitespace as described by the pattern ^\s*$
) and ends one line above the next line containing any character (including a space).
So I think that in your example, when the global command g
finds the empty line number 6, the join command j
looks for a line containing any character (/./
) and finds a space on line 7, then goes back one line above (-
; line 6 again) to determine the final line to collapse. Therefore j
joins the lines between current line 6 and final line 6, which means it doesn't join anything.
After that it joins line 7 and 8.
If you want the join
command to ignore the space when searching for the last line to collapse, you could replace .
(any character) with \S
(any non whitespace character).
It would give:
g/^\s*$/,/\S/-j
A more readable version of the same command could be:
global/^\s*$/ .,/\S/-1 join
The dot .
in this last command is used in the range .,/\S/-1
and not in a search pattern, so this time it doesn't stand for any character but for the current line.
And .,/\S/-1
is a range like a,b
where a
= .
and b
=/\S/-1
.
It could be read like this:
global command (global
), whenever you find an empty line (/^\s*$/
) execute the join
command between the current line (.
) and one line above (-1
) the next line containing no whitespace (/\S/
).