2

In order to load functions in zsh, everything I read says that each function needs to be in separate file and that file needs to be the same name last the function. the path to that function then also needs to be in your $fpath.

Is it possible to have multiple functions in one file in $fpath?

For instance:

    # rsync transfer for DAMS uploads
function rsyncDAMS () {
    rsync -avvPhi --no-p --stats "${@}"
}
export -f rsyncDAMS

    # general rsync transfer
function rsyncT () {
    rsync -avvPhi --stats "${@}"
}
export -f rsyncT

    # general rsync transfer with deletion of source files
function rsyncD () {
    rsync -avvPhi --remove-source-files --stats "${@}"
}
export -f rsyncD

instead of three separate files called rsyncDAMS, rsyncT and rsyncD

could I have one file called rsync_functions with all three of those functions in it and be able to call each function separately by its name? I'd put the ```rsync`` files in my $fpath but I've read nothing that says this will work.

Will it?

Bleakley
  • 653
  • 2
  • 9
  • 19
  • Who said such a thing? – mashuptwice Apr 04 '22 at 05:14
  • You could always `source` a file with all your function definitions into your `.zshrc` – mashuptwice Apr 04 '22 at 05:16
  • @mashuptwice when I do that, it works but it prints all the functions to the terminal every time it is sourced. – Bleakley Apr 04 '22 at 12:57
  • @mashuptwice this post for one - https://dev.to/lukeojones/1up-your-zsh-abilities-by-autoloading-your-own-functions-2ngp?signin=true but I've read it many other places too. – Bleakley Apr 04 '22 at 13:01
  • 1
    Separate files and `fpath` etc are part of autoload for functions. That's an advanced feature that's often used for libraries; it's not needed for the convenience functions that most of us write. You can source the file as @mashuptwice suggested - just get rid of the export, [it isn't needed in zsh](https://unix.stackexchange.com/questions/59360/what-is-the-zsh-equivalent-of-bashs-export-f). – Gairfowl Apr 04 '22 at 14:23
  • For an autoload function, you can only have one 'public' function in a file, but you can add helper functions (which end up being public in their own way). [This answer](https://unix.stackexchange.com/a/33898/432774) has a detailed explanation. – Gairfowl Apr 04 '22 at 14:27

0 Answers0