Search code examples
regexregexp-replace

Find each variable before comma


I am working with a QMK layout in emacs and would like to query-replace-regexp each variable before and including the comma with XXXXXXX,.

I have:

MO(RST), KC_1,    KC_2,    KC_3,    KC_4,    KC_5,                      KC_6,    KC_7,    KC_8,    KC_9,    KC_0,    RESET, \

and would like to end up with:

XXXXXXX, XXXXXXX,    XXXXXXX,    XXXXXXX,    XXXXXXX,    XXXXXXX,                      XXXXXXX,    XXXXXXX,    XXXXXXX,    XXXXXXX,    XXXXXXX,    XXXXXXX, \

I have tried entering .+, but that highlights everything up to the last comma.

I want to stop at each comma, ignoring white space. Meaning first MO(RST),, then KC_1,, etc.. Including the comma in the match is not a necessity.

As a bonus it would be nice to replace the whitespace within a 7-char width to end up with the following spacing:

XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,                   XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, \

Solution

  • I really like Tim's answer, but I can't mark it as the solution since it doesn't work in emacs. What I've been doing instead is:

    Find: [A-Z_].......
    Replace: XXXXXXX,
    

    Find, starting at an alphanumeric character, or _, 8 characters total and replace those 8 characters with XXXXXXX,.

    _ as in _______ which uses the keycode from the layer below. 8 characters total: start to comma inclusive, or if the keycode is less than 7 characters, the whitespace after to maintain alignment.

    This satisfies the bonus condition.