How to make a permanent alias in oh-my-zsh?

192,796

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
Share:
192,796

Related videos on Youtube

Adaephon
Author by

Adaephon

Updated on September 18, 2022

Comments

  • Adaephon
    Adaephon over 1 year

    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?

    • Admin
      Admin about 8 years
      Judging from the (mutliple) comments and from the error I suppose each alias is actually set on a single line, correct?
  • Timo
    Timo over 6 years
    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.
  • nanono
    nanono about 4 years
    Isn't that AUTO_CD cause your command history unreadable?
  • Adaephon
    Adaephon about 4 years
    @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".
  • bora89
    bora89 over 3 years
    Special thanks for shortening directories names - I have been looking for it