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
Create ~/.config/fish/functions/x.fish
:
function x
if [ (pwd) = "/path/to/project` ]
./x.py
else
command x
end
end
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 else
block.
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 ~/.config/fish/conf.d/create_x.fish
:
function create_x --on-variable PWD
if [ "$PWD" = "/path/to/project" ]
function x
./x.py
end
else
functions --erase x
end
end
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.
Prompt-based function
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.
The 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 :-)
fish
, you should write your owncd
function that does whatcd
does (handles "cd ~
" and "cd
", does the actual "cd
"), then decides to "alias
" or "unalias
", based on the directory name or the existence of a file in the destination directory.