29

I know that you can do the following to sort-lines in emacs without case sensitivity:

M-x set-variable [RETURN] sort-fold-case [RETURN] t [RETURN]
M-x sort-lines
M-x set-variable [RETURN] sort-fold-case [RETURN] nil [RETURN]

But this is annoying to do every time. How can I turn this into a function so that I don't have to do the same thing over and over?

phils
  • 71,335
  • 11
  • 153
  • 198
Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134

2 Answers2

33

Pretty straightforward:

(defun sort-lines-nocase ()
  (interactive)
  (let ((sort-fold-case t))
    (call-interactively 'sort-lines)))
abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • 2
    FTR, right after the `(interactive)` line there should be a line `(defvar sort-fold-case)` *(optionally with the default value)*. That is because in this case `sort-fold-case` is a lexical variable, so it's kinda UB that it worked before. And byte-compiling without a `defvar` will result in a complaint `Unused lexical variable ‘sort-fold-case’`. On top of that, latest Emacs built from git will even bail out on that with `custom-declare-variable: Defining as dynamic an already lexical var`. – Hi-Angel Jul 06 '21 at 14:23
9

If you always want to sort case insensitive, try this in a file used on startup:

(custom-set-variables
 '(sort-fold-case t t)
)

Then you can just call M-x sort-lines.

maurits
  • 2,355
  • 13
  • 16