~/.zshrc alias with a space

5.8k Views Asked by At

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?

3

There are 3 best solutions below

1
On BEST ANSWER

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'"
0
On

Considering the confusion caused by backslash inside single or double quotes, here is one alternative :

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

You need two backslashes.

$ mkdir "f oo"
$ alias f="cd f\\ oo"
$ f
$ pwd
/home/foobar/tmp/f oo