Ubuntu – How to make a permanent alias in oh-the-zsh

aliascommand linezsh

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?

Best Answer

  • 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
    
  • Related Question