1

I am writing a custom module to extend the zprezto framework for myself. The goal is to be able to start a tmux with proper configuration by name. Here is the code of the init.zsh of my custom module:

SCRIPT=${0:A}
export _MYTMUX_SDIR=`dirname $SCRIPT`

function start_tmux {
    PROJECT_NAME=$1
    tmux has-session -t $PROJECT_NAME 2>/dev/null
    if [ "$?" -eq 1 ] ; then
        echo "No Session found. Creating and configuring..."
        tmux new-session -d -s $PROJECT_NAME
        tmux source-file ${_MYTMUX_SDIR}/tmux-${PROJECT_NAME}.conf
    else
        echo "Session found. Connecting."
    fi
    tmux attach-session -t $PROJECT_NAME
}


function start_aws {
    start_tmux aws
}

function start_digitalocean {
    start_tmux digitalocean
}

I am forced to export a variable _MYTMUX_SDIR to keep track of the location of the script. It is needed because the tmux configuration files are colocated with the module init.zsh script.

If I put this code inside the function body, it will give me the directory location wherein the function is called.

My question:

Is there a better way to find out the script location? I want to avoid putting variable in environment unnecessarily.

Anthony Kong
  • 862
  • 2
  • 14
  • 23

0 Answers0