How to save user made zsh shell functions and aliases

11,442

Solution 1

What's the procedure for saving user defined functions and aliases for future use?

You probably want to save your customisations in ~/.zshrc.

~/.zshrc is always executed when zsh starts (both interactive logins and non-interactive logins).


Further reading

  • zsh - Installation and Configation, links to example configuration files.

Solution 2

Restoring from file is already answered.
For saving aliases to file, alias -L will list your aliases in command form. This can easily be redirected to any file you wish, ready to be sourced from ~/.zshrc or elsewhere.

I always do this via a super-useful "save aliases" alias:

alias sa="alias -L > ~/.zsh_aliases"

This way I can add new aliases on the command line and immediately save them on the fly, without opening an editor or thinking about config files. Note that removals of anything coming from the file will also be persisted.

Share:
11,442

Related videos on Youtube

Juho Karppinen
Author by

Juho Karppinen

Updated on September 18, 2022

Comments

  • Juho Karppinen
    Juho Karppinen over 1 year

    Shell beginner here! I'd like to create aliases and functions in zsh shell to speed up my workflow. For example:

    tw() { open -a //Applications/Textwrangler.app "$*"; }
    

    The function itself works, but the problem is that the function definition disappears once I exit the shell. What's the procedure for saving user defined functions and aliases for future use? The zsh documentation isn't particularly helpful... or maybe I'm just being stupid!

  • Juho Karppinen
    Juho Karppinen about 9 years
    Thanks! Since I have oh-my-zsh installed, a comment inside of ~/.zshrc suggested I add my customizations to the ~/.oh-my-zsh/custom folder, using any filename with a .zsh extension.