Sorting lines in Emacs is fairly straightforward with sort-lines
.
However, how do I sort within a line? For example: John Ringo Paul George
-> George John Paul Ringo
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityEvaluate the following form (or put it into your .emacs
):
(defun your/sort-line ()
"Sort the current line as you wish."
(interactive "*") ; Complain if buffer is read-only.
(insert (mapconcat #'identity
(sort (split-string (prog1 (buffer-substring-no-properties (line-beginning-position) (line-end-position))
(delete-line)))
#'string<)
"\s")))
Type M-x your/sort-line
when the cursor is at the line you want to sort.
There are several sorting commands, and some let you specify how you want "fields" to be defined, and they sort fields. See, for example, sort(-regexp)-fields
and sort-numeric-fields
, and maybe even sort-columns
, depending on what you need.
See the Elisp manual, node Sorting.