Search code examples
macoszshzshrc

~/.zshrc alias with a space


I have a bad habit of putting spaces in my folder/file names. Today it bites me.

I have a folder called NFB Lab in which I installed NFB Lab. I wanted to add the shortcut/command nfb and pynfb to the ~/.zshrc file to start the main python script from anywhere.

I edited the ~/.zshrc file through nano with:

alias nfb=/Users/mathieu/Documents/NFB\ Lab/pynfb/main.py
alias pynfb=/Users/mathieu/Documents/NFB\ Lab/pynfb/main.py

I also tried:

alias nfb="/Users/mathieu/Documents/NFB Lab/pynfb/main.py"
alias pynfb="/Users/mathieu/Documents/NFB Lab/pynfb/main.py"

Neither works, I always get:

zsh: no such file or directory: /Users/mathieu/Documents/NFB

How can I solve this without uninstall/reintsall of NFB Lab?


Solution

  • You'll need to escape the space (\ ), for example, take a look at my alias;

    alias sub='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
    

    Otherwise, take a look at ZSH functions. There are many more options compared to aliasses;

    For example, run python script with arg as path, then create an alias calling that function

    function runpy() {
        python3 "$@"
    }
    alias runx="runpy '/tmp/dir with space/py.py'"
    alias runy="runpy '/tmp/dir with space/py_second.py'"