Suppose I have the following text:

int z;
int a;
int c;
int g;

or the following text:

z, a, c, g

How can I sort them to:

int a;
int c;
int g;
int z;


a, c, g, z


1 Answer 1


Use M-x sort-lines for the first example. For the second example, change it into

z, a, c, g,

then use M-x sort-regexp-fields <RET> ., <RET> \& <RET>.

  • 1
    Note that ., is specific to this example, if you need more generic patterns for coma separated words, you can use \s-?\w+,?
    – Seki
    Mar 6, 2023 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.