1

I am trying to use fish as my default interactive shell. I was previously using bash and there was support for alias named - (ASCII hyphen, 0x2d). Why does not fish allow creation of alias or function with such name? As far as I know, it has no special meaning in fish.

Bash-like invocation of alias is not possible:

fish-3.1.2$ alias -- -='aliased command'
- (line 1): function: Illegal function name '-'
function - --wraps 'aliased command' --description 'alias -=aliased command';  aliased command $argv; end
^
from sourcing file -
        called on line 61 of file /usr/share/fish/functions/alias.fish
in function 'alias' with arguments '-- -=aliased\ command'
3

2 Answers 2

2

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.

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .