how to sort text, lines and fields in the emacs editor
Emacs has several different ways of sorting text in the buffer. You can sort lines, paragraphs and also sort text based on fields or words in the line.
Sort Lines
This is probably the simplest of all sorting method. The sort-lines command sorts all lines in the active region. Select the region in the buffer that you want to sort and execute the command …
M-x sort-lines
The sort-lines command will sort the lines in the active region alphabetically. By default, there is no key mapped to the command. However, you can easily bind a shortcut of your choice to the command if you like.
(global-set-key (kbd "C-x C-;") 'sort-lines)
As with almost all commands in emacs, this works on the currently active region of the buffer. If you would like to sort the entire buffer, then select the entire buffer first…
C-x h M-x sort-lines # This will sort all lines in the buffer
Reverse sorting
By default the sorting is lexicographic, case-sensitive and in ascending order. If you want to sort the lines in reverse order, there are a couple of different options. You can use C-u to negate or reverse the order of the sort.
C-u M-x sort-lines # This will sort lines in reverse order
The other option you have is to reverse the order of lines in the region after it is sorted. You can do that with the reverse-region command.
M-x reverse-region # Reverses the order of the lines in the region
Case-insensitive sorting
By default the sorting is case-sensitive. This is controlled by the variable sort-fold-case. In order to have case insensitive sorting, set the variable to t
M-x set-variable [RET] sort-fold-case [RET] t [RET]
Numeric sorting
As mentioned earlier, the default sorting is lexicographic. If you want to have numeric sorting then you will use the command sort-numeric-fields
M-x sort-numeric-fields
You can perform the reverse sorting by reversing the region after sorting as described in the previous section.
M-x sort-numeric-fields M-x reverse-region
Sort Paragraphs
You can sort the paragraphs in the active region using the sort-paragraphs command. The definition of the paragraph might vary depending on the mode you are using. But usually anything text that is separated by one or more new line is considered as a paragraph.
M-x sort-paragraphs
You can reverse the sort order by using the C-u prefix before the sort command.
C-u M-x sort-paragraphs
Sort Fields
A field is defined as text within a line that are separated by any whitespace. This is very useful when working with tabulated data. You can sort the fields by specifying the numeric prefix to the sort-fields command. So if you want to sort the second field then you can do the following
C-u 2 M-x sort-fields
As before, you reverse the sort order by using the reverse-region command.
C-u 2 M-x sort-fields M-x reverse-region
There is an equivalent sort-numeric-fields command for the numeric sorting of the fields that we mentioned earlier in the post. You can use a numeric prefix to that command to specify which field you want to sort by.