zsh config & automatic function loading

151 Views Asked by At

I created a folder inside my .config/zsh called .zsh_functions while building alacritty from source to add alacritty functions inside of it which worked as expected.

I'm like cool, by adding fpath+=${ZDOTDIR:-~}/.zsh_functions to my .zshrc I can throw a file which contains a function and zsh will pick it up and I don't have to create an alias for it.

Also I will note here that I do have zdotdir set inside my .zshenv which is located in my home dir with:

ZDOTDIR=$HOME/.config/zsh

I created a file called my_functions with a simple function:

myserver() {
    typeset username="username"
    typeset host="128.0.0.1" #changed the IP for illustration purpose

    ssh "$username@$host"
}

Now, although the alacritty functions works, my function doesn't unless I for loop my .zsh_functions folder

for file in "${ZDOTDIR:-~}"/.zsh_functions/*(.); do
    source "$file"
done

I tried adding this instead of a for loop:

# Add .zsh_functions to path
autoload -Uz compinit && compinit
fpath+=${ZDOTDIR:-~}/.zsh_functions

When I echo $FPATH I see that .zsh_functions folder is being included into $FPATH, but yet the function is not recognized. Even when I type typeset -f myserver I get nothing.

So I wrote all that to ask you this, do I got to explicitly source all files within my .zsh_functions , or should including it to the $FPATH enough?

Some plugins and functions in zsh is very sensitive where

autoload -Uz compinit && compinit

is placed inside .zshrc

I also tried:

chmod +x my_functions

Nothing seems to work until I do the for loop which seems to be redundant since I'm adding it to the fpath.

Any clarification would help, thanks in advance!

0

There are 0 best solutions below