Sorting String In A Line

Charles Choi has posted a tip mostly, he says, as a reminder to his future self. It’s about how to sort strings on a single Emacs line. At first blush that may not seem all that useful but a moment’s thought will bring to mind all sorts of situations where it’s just what you need.

The secret sauce is sort-regexp-fields, which actually sorts a region whether or not it’s on a single line. When called interactively, you will need to supply two regexes. The first is used to identify the record and the second to identify the key of that record.

If you specify a match field in the record regex, you can use a match number (\n) for the key regex. If the key is the whole field, you can use \&.

Choi has an example that shows how to sort quote-delimited strings on a single line and the documentation has an example of sorting lines by the first word that starts with f. The documentation example shows how powerful the method really is.

In any event, this post is partially a reminder to future me and you should bookmark Choi’s post to remind yourself how to do it. It’s another one of those things that you don’t need very often but when you do, you’ll be really glad to have a reminder of how to do it.

This entry was posted in General and tagged . Bookmark the permalink.