From 53111ba9f88e9843416f2f868b25b99b06dbc48f Mon Sep 17 00:00:00 2001 From: Arto Jantunen Date: Sun, 15 Feb 2026 08:49:26 +0200 Subject: [PATCH] Sort Eshell aliases when adding new ones The main point is to make the content of 'eshell-aliases-file' stable so that storing it in git becomes feasible. This also makes the output of the alias command come out in alphabetical order (bug#80401). * lisp/eshell/em-alias.el (eshell/alias): Sort when updating 'eshell-command-aliases-list'. * test/lisp/eshell/em-alias-tests.el (em-alias-test/alias-list-is-sorted): New test. * etc/NEWS: Announce this change. --- etc/NEWS | 7 +++++++ lisp/eshell/em-alias.el | 4 +++- test/lisp/eshell/em-alias-tests.el | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/etc/NEWS b/etc/NEWS index d05fed3f8d8..1e6756e431e 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1977,6 +1977,13 @@ only search in input history. If you customize it to the symbol 'dwim', those commands search in input history only when the point is after the last prompt. +--- +*** Eshell 'alias' command now sorts the alias list. +When adding an alias interactively, Eshell now sorts the list of aliases +before saving the alias file. This maintains the stability of the +list of aliases to make the diff between versions more readable if you +store your aliases in version control. + ** Mail Utils +++ diff --git a/lisp/eshell/em-alias.el b/lisp/eshell/em-alias.el index b4b2d61505b..43da7a77430 100644 --- a/lisp/eshell/em-alias.el +++ b/lisp/eshell/em-alias.el @@ -173,7 +173,9 @@ file named by `eshell-aliases-file'.") (setq eshell-command-aliases-list (delq def eshell-command-aliases-list))) (setq eshell-command-aliases-list - (cons alias-def eshell-command-aliases-list)))) + (sort (cons alias-def eshell-command-aliases-list) + (lambda (a b) + (string< (car a) (car b))))))) (eshell-write-aliases-list)) nil) diff --git a/test/lisp/eshell/em-alias-tests.el b/test/lisp/eshell/em-alias-tests.el index 02c3c1f2518..c235c4d5f51 100644 --- a/test/lisp/eshell/em-alias-tests.el +++ b/test/lisp/eshell/em-alias-tests.el @@ -93,4 +93,13 @@ (eshell-insert-command "alias add-funny-pair '+ $*[0][: 0] $*[1][: 1]'") (eshell-match-command-output "add-funny-pair 1:2 3:4" "5\n"))) +(ert-deftest em-alias-test/alias-list-is-sorted () + "Test that the alias list is sorted on insert" + (with-temp-eshell + (eshell-insert-command "alias aaa 'aaa'") + (eshell-insert-command "alias ccc 'ccc'") + (eshell-insert-command "alias bbb 'bbb'") + (eshell-match-command-output + "alias" "alias aaa aaa\nalias bbb bbb\nalias ccc ccc\n"))) + ;; em-alias-tests.el ends here