Three possibilities that I can think of (other than the ones that Damir suggested). First, if at all possible, I'd just test whether or not you are in the directory in a "lazy-load" function. This is similar to what Damir recommended, but it avoids the overhead of being in your startup config or in an executable script.
Lazy-load function-based solution
if [ (pwd) = "/path/to/project` ]
This function will only load when called for the first time via
x. This avoids the overhead of adding it to your startup config. It is also still a function, so it executes in the current
fish shell, rather than starting a new shell like an executable script would.
It also falls back to any other
x command that might be installed on the system in case you aren't in that directory. If you don't need this, just delete the
Function which is created when you enter the project directory
If you really need to have the function only exist when you are in that directory, there are two more alternatives. First, fish functions can watch for a variable to change, and only run when it does. That variable can be
$PWD to watch for a directory change.
Add the following in
function create_x --on-variable PWD
if [ "$PWD" = "/path/to/project" ]
functions --erase x
This does require the
create_x.fish function to be loaded at startup, but it will only run when you change directories. It's definitely less efficient than the first option.
Finally, you can modify your prompt function to check the current directory. This seems wasteful, but:
This will check to see if you are in the project directory on each prompt, and will only create the function if you are.
funcsave places a copy of the "normal"
fish_prompt in your
~/.config/fish/functions directory. Delete it to return to the normal prompt functionality.
Definitely, go with the first option if you can :-)