PollPenn
PollPenn

Reputation: 733

Sublime Text 3: How to do multi-line selection that excludes blank lines?

I have some scripts in Stata written as follows:

* 1. count of firms in each bin
grouplabs Inear_dist_km_0_10 Inear_dist_km_10_30 Inear_dist_km_30_60 Inear_dist_km_60_100 Inear_dist_km_100_150 Inear_dist_km_morethan150, groupvar(Inear_dist_km_gr) emptylabel(empty)
graph hbar (count) if Inear_dist_km_gr !=1, over(Inear_dist_km_gr) name(n1)

* 2. count of firms in each bin (bigger bins)
grouplabs Inear_dist_km_0_20_v2 Inear_dist_km_20_40_v2 Inear_dist_km_40_60_v2 Inear_dist_km_morethan60_v2, groupvar(Inear_dist_km_v2_gr) emptylabel(empty)
graph hbar (count) if Inear_dist_km_v2_gr !=1, over(Inear_dist_km_v2_gr) name(n2)

* 3. GGK firm level bins
grouplabs Inear_dist_km_GGK_0_10 Inear_dist_km_GGK_10_50 Inear_dist_km_GGK_morethan50, groupvar(Inear_dist_km_GGK_gr) emptylabel(empty)
graph hbar (count) if Inear_dist_km_GGK_gr !=1, over(Inear_dist_km_GGK_gr) name(n3)

I need to add the character ; at the end of each line, but not in the blank lines between each script. I have tried the Split into Lines trick to get multiple cursors at the end of each line, but the selection also includes the empty lines in between. This results in

* 1. count of firms in each bin;
grouplabs Inear_dist_km_0_10 Inear_dist_km_10_30 Inear_dist_km_30_60 Inear_dist_km_60_100 Inear_dist_km_100_150 Inear_dist_km_morethan150, groupvar(Inear_dist_km_gr) emptylabel(empty);
graph hbar (count) if Inear_dist_km_gr !=1, over(Inear_dist_km_gr) name(n1);
;
* 2. count of firms in each bin (bigger bins);
grouplabs Inear_dist_km_0_20_v2 Inear_dist_km_20_40_v2 Inear_dist_km_40_60_v2 Inear_dist_km_morethan60_v2, groupvar(Inear_dist_km_v2_gr) emptylabel(empty);
graph hbar (count) if Inear_dist_km_v2_gr !=1, over(Inear_dist_km_v2_gr) name(n2);
;
* 3. GGK firm level bins;
grouplabs Inear_dist_km_GGK_0_10 Inear_dist_km_GGK_10_50 Inear_dist_km_GGK_morethan50, groupvar(Inear_dist_km_GGK_gr) emptylabel(empty);
graph hbar (count) if Inear_dist_km_GGK_gr !=1, over(Inear_dist_km_GGK_gr) name(n3);

How can I exclude the empty lines from the selection so that only lines with characters in it will have ; at the end? I appreciate your help.

Upvotes: 0

Views: 1411

Answers (1)

Jakub Kozakoszczak
Jakub Kozakoszczak

Reputation: 61

  1. If you're sure that your blank lines are really blank and not filled with whitespace characters use regex substitution by pressing ctrl+H, search for (?<!^)$ and replace with ;.

    • The regex says: find an end of line ($) not directly after ((?<!…)) the beginning (^).
  2. Else search for (?<=\S)$ but make sure you don't havy any trailing spaces or tabs.

    • How to 'make sure': find them with [^\S\n]+$ and delete.
    • The first regex says: find an end of line directly after ((?<=…)) a non-whitespace character, including linebreak.

    • The second regex says: find a sequence (…+) of characters that are not any of ([^…]) non-whitespace characters or linebreak -- i.e. a sequence of whitespace characters excluding the linebreak -- and then the end of line.

  3. The most visual and friendly way might be to
    • find your blank lines with ctrl+F, search for ^\s*$,
    • select all with alt+enter,
    • menu > Selection > Invert Selection (now you've highlighted inly the non-blank lines),
    • go on with the original Split into Lines trick.

Upvotes: 2

Related Questions