@@ -129,6 +129,10 @@ def __subclasshook__(cls, C):
129129# All help functions start with this
130130HELP_FUNC_PREFIX = 'help_'
131131
132+ # Sorting keys for strings
133+ ALPHABETICAL_SORT_KEY = utils .norm_fold
134+ NATURAL_SORT_KEY = utils .natural_keys
135+
132136
133137def categorize (func : Union [Callable , Iterable ], category : str ) -> None :
134138 """Categorize a function.
@@ -492,6 +496,12 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
492496 if os .path .exists (startup_script ) and os .path .getsize (startup_script ) > 0 :
493497 self .cmdqueue .append ("load '{}'" .format (startup_script ))
494498
499+ # The default key for sorting tab completion matches. This only applies when the matches are not
500+ # already marked as sorted by setting self.matches_sorted to True. Its default value performs a
501+ # case-insensitive alphabetical sort. If natural sorting preferred, then set this to NATURAL_SORT_KEY.
502+ # Otherwise it can be set to any custom key to meet your needs.
503+ self .matches_sort_key = ALPHABETICAL_SORT_KEY
504+
495505 ############################################################################################################
496506 # The following variables are used by tab-completion functions. They are reset each time complete() is run
497507 # in reset_completion_defaults() and it is up to completer functions to set them before returning results.
@@ -520,7 +530,7 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
520530 self .matches_delimited = False
521531
522532 # Set to True before returning matches to complete() in cases where matches are sorted with custom ordering.
523- # If False, then complete() will sort the matches alphabetically before they are displayed.
533+ # If False, then complete() will sort the matches using self.matches_sort_key before they are displayed.
524534 self .matches_sorted = False
525535
526536 # Set the pager(s) for use with the ppaged() method for displaying output using a pager
@@ -1119,8 +1129,8 @@ def complete_users() -> List[str]:
11191129 self .allow_appended_space = False
11201130 self .allow_closing_quote = False
11211131
1122- # Sort the matches alphabetically before any trailing slashes are added
1123- matches .sort (key = utils . norm_fold )
1132+ # Sort the matches before any trailing slashes are added
1133+ matches .sort (key = self . matches_sort_key )
11241134 self .matches_sorted = True
11251135
11261136 # Build display_matches and add a slash to directories
@@ -1560,10 +1570,10 @@ def complete(self, text: str, state: int) -> Optional[str]:
15601570
15611571 self .completion_matches [0 ] += str_to_append
15621572
1563- # Sort matches alphabetically if they haven't already been sorted
1573+ # Sort matches if they haven't already been sorted
15641574 if not self .matches_sorted :
1565- self .completion_matches .sort (key = utils . norm_fold )
1566- self .display_matches .sort (key = utils . norm_fold )
1575+ self .completion_matches .sort (key = self . matches_sort_key )
1576+ self .display_matches .sort (key = self . matches_sort_key )
15671577 self .matches_sorted = True
15681578
15691579 try :
0 commit comments