50

I'd like to have a file eg. f with only zsh aliases (pureness reasons). Then I'd like to include f file in my .zshrc file, so that the aliases defined in f are visible in .zshrc.

Is it possible?

If it is, I could create a script eg. my_alias ($my_alias ll 'ls -l') which appends alias to f file. Of course I could do

$echo {alias command} >> ~/.zshrc

but this makes .zshrc one big mess.

Additionally how is it looks like in bash?

UPDATE

If someone share my idea this is solution, thanks to phunehehe:

# source aliases
ALIASFILE=~/.aliasesrc
source $ALIASFILE
function add_alias() {
    if [[ -z $1 || -z $2 || $# -gt 2 ]]; then
        echo usage:
        echo "\t\$$0 ll 'ls -l'"
    else
        echo "alias $1='$2'" >> $ALIASFILE
        echo "alias ADDED to $ALIASFILE"
    fi
}
0

5 Answers 5

61

This is, how I do it in my .zshrc:

if [ -f ~/.zsh/zshalias ]; then
    source ~/.zsh/zshalias
else
    print "404: ~/.zsh/zshalias not found."
fi
1
  • Thanks for good answer, You and phunehehe are correct but it's only possible to approve one answer so I decided to give it to phunehehe becasue he was first :)
    – xliiv
    Commented Oct 14, 2012 at 16:04
34

.zshrc and .bashrc are script files, not config files, so you "source" the alias file. In Zsh (.zshrc) and Bash (.bashrc) alike:

. my_alias

will run my_alias and leave its effects in the same environment with the RC files, effectively giving you the aliases in the shell. Of course, your are not limited to aliases either. I use a .shrc that is sourced by both .bashrc and .zshrc for common exports, functions and aliases.

For more on sourcing see Different ways to execute a shell script.

1
  • This does not work. When ~/.zshrc is a symlink, and my_alias is a file in the directory local to the referent file (not located at ~/my_alias), this command fails. /Users/me/.zshrc:.:3: no such file or directory: my_aliases Commented Feb 4, 2022 at 3:20
23

To source a file if it exists in one line:

[ -f .aliases ] && source .aliases
2
  • How is this any different to pat's answer?
    – jasonwryan
    Commented Mar 18, 2015 at 3:03
  • 3
    Same thing but one-liner. I came from google and the one liner was what I was looking for (found it on a forum at the same time) so I thought I might as well share that ;) I wouldn't have been able to come up with it as I'm dumb in bash Commented Mar 18, 2015 at 9:03
4

oh-my-zsh automatically will scan files in the $ZSH_CUSTOM directory looking for any that end with .zsh. If you create a file called aliases.zsh and place it in that directory, it will automatically load those aliases. If you need to override existing oh-my-zsh aliases, then you would configure them at the bottom of the .zshrc file.

1
  • best answer. there is also an example.zsh in that folder exactly what I was looking for
    – Carmen
    Commented Oct 17, 2022 at 15:37
4

After installing oh-my-zsh the last lines of .zshrc are:

# 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"

So you have some alternatives:

  • Add directly to .zshrc

  • Create a folder and add at its path at the end of .zshrc: ZSH_CUSTOM=$HOME/Dropbox/.myzshconfig.

  • Use the ~/.oh-my-zsh/custom folder and git clone git://github.com/yourusername/zsh_config.git . there.

  • With source/dot operator:

    # CUSTOMIZATION FOR ZSH
    if [ -r ~/Dropbox/.myzshrc ]; then
      source ~/Dropbox/.myzshrc
    fi
    # Or [ -r ~/Dropbox/.myzshrc ] && source ~/Dropbox/.myzshrc
    

    -r FILE check if exists and read permission is granted.

    In this case you could check $SHELL to custom bash and zsh including the same file.

Reference

1
  • Thanks for the Nice idea of Dropboxing it Commented Mar 19, 2019 at 3:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .