Why fish shell does not allow function named `-`

Solution 1:

Aliases are just a wrapper around fish functions, so I can confirm in Fish 3.3.1 (current version as of this post), this won't work:

function -
   echo "here"
end
fish: function: Illegal function name '-'

However, there's a simple workaround by using abbr (abbreviations):

abbr -a -- - 'cd -'

In case it's unclear what this is doing with all the dashes, the double dash "--" separates ambiguous command options from arguments so that the following dash "-" is recognized as the name of the abbr, and not interpreted as another option like "-a".

In many ways, abbreviations are better than aliases anyway, as they don't mask the real command you executed in your history, and setting up a lot of aliases in your fish.conf is slow, as all those aliases get eval-ed to functions anyway, but don't get the benefit of being lazy loaded. The fish docs also confirm abbr is the way.

Solution 2:

I went digging in the history, and it looks like this was introduced in 320cb6857f to fix an issue where function names starting with - were interpreted as arguments. Functions starting with - didn't actually work, but this change made it explicit.