1

How do you manage your bash/zsh Aliases?

Is there a tool to do this more easily and less "think of an alias -> open .zshrc-sourced-file -> edit -> close, reload .zshrc -> use alias"?

2 Answers 2

2

I rarely add or edit my aliases any more, but I still have and occasionally use:

alias savealiases='alias > ~/.bash-aliases'

I also have loadaliases, so I can create or change an alias in one shell (terminal window or tab, tmux window, ssh session, etc) and easily load it into other running shells.

alias loadaliases='source ~/.bash-aliases'

To make sure my aliases are loaded when I login or start up a new shell, I have the following in my ~/.bashrc:

[ -e ~/.bash-aliases ] && . ~/.bash-aliases

Extremely primitive but it's worked well enough for me for over 20 years. If I need to do any fancy editing of my aliases, I have vi.

2
  • Note: In Zsh you need to use alias -L to get them in sourcable format. My trusty sa-alias is always the first thing I create in a new account. Surprised it is not more common. Commented Jan 14, 2021 at 8:14
  • @JoachimLous thanks for the zsh -L comment. And, yeah, this was a pretty common (and commonly recommended) thing to do back in the 90s. I prefer the long forms, savealiases and loadaliases, because I use them so infrequently and don't want to waste valuable 2 or 3 letter command names on them or conflict with other programs. It's also easy to remember: "I want to save my aliases, what's that command called again?" :-)
    – cas
    Commented Apr 6, 2021 at 2:51
-2

If you are not only interested in bash and zsh, you may be interested in a completely different method that was introduced by the UNOS command interpreter in 1980. This command interpreter implements persistent aliases that are stored in the file $HOME/.globals

This method is now available in bsh and in recent Bourne Shell version you may get from the Schily-Tools.

Please have a look at the recent Bourne Shell man page at:

http://schillix.sourceforge.net/man/man1/bosh.1.html

The reason why I mention this method is that complex alises in the Korn Shell syntax are hard to type with the correct quoting and the Bourne Shell implements (as an additional method besides alias) so called hash commands that allow to enter aliases in raw mode.

BTW: the correct way to save aliases for other shells that do not support persistent aliases is:

alias -p > file

If you just call alias with ksh93, you get output like this:

autoload='typeset -fu' command='command ' compound='typeset -C'

but when you call alias -p, you get:

alias autoload='typeset -fu' alias command='command ' alias compound='typeset -C'

As you see, you need alias -p because it returns something that can later be used as shell input again by calling:

. file

Edit: alias -p is not POSIX but no shell except bash seems to produce parsable output by default. Some shells never produce parsable output!

Shells that need -p for a parsableoutput are:

  • Bourne Shell
  • ksh93 (ksh88 does neither support parsable output nor -p)

bash permits -p...

BTW: dash does not support parsable output nor -p at all. zsh implements the equivalent of alias -pvia alias -L.

10
  • AFAICS, there's no mention of alias -p in the POSIX specification for alias, and in practice is only supported in ksh, bash and yash. The question is about bash (where alias works the same as alias -p except in sh emulation, and it's true that in that case alias -p would be needed) and zsh (where -p is not a supported option). Commented Oct 5, 2015 at 9:20
  • OK, I edited my text, but I do not have "yash".
    – schily
    Commented Oct 5, 2015 at 11:02
  • In zsh, you use alias -L. The POSIX output of alias is parsable, but not POSIXly. For instance in bash or zsh. eval "a=($(alias))" would work. Also note that zsh has the different types of alias definitions in associative arrays ($aliases, $galiases...). Commented Oct 5, 2015 at 11:12
  • POSIXly, you could use xargs to process the output of dash's alias, but not that of ksh93 which uses the $'...' form of quoting in some cases. Commented Oct 5, 2015 at 11:18
  • yash is at yash.osdn.jp/index.html.en Commented Oct 5, 2015 at 11:19

You must log in to answer this question.

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