Use Function From Earlier in Function Path

105 views Asked by At

In fish, is it possible to reference or use a function inside another function of the same name? For example, fish has a function ls at /usr/local/share/functions/ls.fish, which sets some default parameters based on things like OS and whether the shell has colors. I want to define another function that will set some default colors for me without clobbering this other function in case I want to use a newer version of fish.

The easiest way to set an alias seems to be to use command, like so:

function ls
    command ls -F $argv
end

in the file ls.fish in the folder ~/.config/fish/functions. This is the default folder for user-defined functions. Both this folder and ~/.config/fish/functions are in $fish_function_path, but ~/.config/fish/functions comes later so users can set their own functions which override fish's default functions. However, as the fish documentation says,

command forces the shell to execute the program COMMANDNAME and ignore
any functions or builtins with the same name.

How do I override an alias/function with one defined later in $fish_function_path, without overwriting it? Ideally, solutions would also use the built-in command of the same name, like cd, as according to the cited documentation that is not possible, either, but this is not required.

Finally, is this a good idea? If fish does not do this by default, there must be a reason, right?

1

There are 1 answers

3
ridiculous_fish On BEST ANSWER

The usual trick is to copy the function you want to override, and then invoke the copy from within the override:

functions --copy ls saved_ls
function ls
    saved_ls
end

You can't do this in an autoloading ls.fish file since it would result in an infinite loop, but you can do it in config.fish.