regex - How can I collapse multiple whitespace lines with vim? -


the question , answers here cover in detail how following vim command collapses series of empty lines single line:

:g/^$/,/./-j 

however, want same treat lines onlywhite space in them blank. following command tried doesn't work:

:g/^\s*$/,/./-j 

as far can tell, should find lines empty , have whitespace on them, not lines being collapsed.

you're halfway there.

remember initial command consisted of search part , action part. search part :g/^$/ found empty lines , action part ,/./-j executed each (well, each hadn't been deleted previous j).

the modification made search part of string correct in find lines either empty or contain whitespace.

however, it's action you're executing after that's causing grief. original action executed on found line ,/./-j means execute join j on range line 1 before next 'real' character. more detail on how works can found in question linked to.

the first 'real' character finds in case includes whitespace so, while search bit find whitespace lines , act on them, range of join in action not want.

what need specify end of range in action line previous next 1 has other whitespace (rather line 'real' character). line non-whitespace character 1 matches regex \s (the backslash uppercase s denotes non-whitespace character).

so, in end, you're looking is:

:g/^\s*$/,/\s/-j 

having said that, keep in mind line remains behind (i think) first range. so, it's not empty, may contain white-space.

if wish ensure whitespace-only lines made empty, execute:

:g/^\s*$/s/.*// 

after collapsing command above. or, can combine both single command using | action separator:

:g/^\s*$/,/\s/-j|s/.*// 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -