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
Evaluate 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.
sort-line
for this, instead of defining a new command, my-sort-line
?
sort-line
for this" -- My Emacs doesn't have a function named sort-line
.
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.