69

In my .zshrc I tried to make a few aliases .I looked into a lot of places, but I couldn't find out a way that worked. I used this code below:

# Set personal aliases, overriding those provided by oh-my-zsh libs, 
# plugins, and themes. Aliases can be placed here, though oh-my-zsh 
# users are encouraged to define aliases within the ZSH_CUSTOM folder. 
# For a full list of active aliases, run alias. # # Example aliases
alias zshconfig="mate ~/.zshrc"
alias ohmyzsh="mate ~/.oh-my-zsh"
alias n= "nano"  
alias m= "mkdir"
alias w= "cd ~/Documents/UoMWorkspace/Semester2"  
alias j= "cd ~/Documents/UoMWorkspace/Semester2/COMP17412"

Then I wrote a command source ~/.zshrc. Still it didn't resolve the issue. I get error messages like zsh: command not found: j

Could anyone help me with any suggestions and let me know what am I doing wrong?

  • 4
    Judging from the (mutliple) comments and from the error I suppose each alias is actually set on a single line, correct? – kos Apr 17 '16 at 22:49
103

There must not be any whitespaces around between = and either alias name or alias definition:

alias zshconfig="mate ~/.zshrc"
alias ohmyzsh="mate ~/.oh-my-zsh"
alias n="nano"
alias m="mkdir"
alias w="cd ~/Documents/UoMWorkspace/Semester2"
alias j="cd ~/Documents/UoMWorkspace/Semester2/COMP17412"

BTW: If you are looking for a way to shorten directory names, I suggest looking into Named Directories and the AUTO_CD option instead of aliases:

hash -d w=~/Documents/UoMWorkspace/Semester2
hash -d j=~/Documents/UoMWorkspace/Semester2/COMP17412

This allows you to use ~w instead of ~/Documents/UoMWorkspace/Semester2 and ~j instead of ~/Documents/UoMWorkspace/Semester2/COMP17412 (or ~w/COMP17412). So cd ~j is identical to cd ~/Documents/UoMWorkspace/Semester2. It also works as part of a path, e.g. cat ~j/somedir/somefile.

With

setopt AUTO_CD

zsh will automatically cd to a directory if it is given as command on the command line and it is not the name of an actual command. e.g.

% /usr
% pwd
/usr
% ~w
/home/YOURUSERNAME/Documents/UoMWorkspace/Semester2
| improve this answer | |
  • 1
    As a further explanation why not use spaces before and after the equation sign:e.g. a = b: This will be considered as a command a with two parameters (b and =) and will throw an error: zsh: command not found: a. If you put aliasbefore that (alias a = b), it will be somehow similar. – Timo Oct 22 '17 at 7:29
  • 1
    Isn't that AUTO_CD cause your command history unreadable? – nanono Apr 18 at 3:34
  • 2
    @nanono That depends on what you mean by "unreadable". By itself AUTO_CD does nothing more than allow to switch into directories by only using their paths instead of using the cd command. Of course, if you make active use of this feature, it is not easy to tell from the history whether the command docs ran a executable of that name or if it was used to switch into a directory named "docs". – Adaephon Apr 21 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.