Search code examples
zshfish

How do I convert this zsh function to fish shell?


I have this function which works great in zsh, but I want to convert it to fish shell and I can't get it working.

function ogf () {
  echo "Cloning, your editor will open when clone has completed..."
  source <(TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts "$1")
}

Solution

  • First of all, since fish's syntax differs from zsh, you also have to change the output of clone_git_file to source it.

    For example, if clone_git_file is something like:

    #!/bin/bash
    echo "FOO=$TARGET_DIRECTORY"
    echo "BAR=$2"
    

    you have to change it to fish syntax.

    #!/bin/bash
    echo "set -gx FOO $TARGET_DIRECTORY"
    echo "set -gx BAR $2"
    

    Now here's the ogf() function, and sample code for fish:

    function ogf
      echo "Cloning, your editor will open when clone has completed..."
      source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
    end
    
    ogf MY_ARGUMENT
    echo "FOO is $FOO"
    echo "BAR is $BAR"
    

    Running this code with fish, the output is:

    FOO is /home/MY_USER/students
    BAR is MY_ARGUMENT