3

I'd like to be able to quickly, on-the-fly add (and list/remove) command aliases so that they persist between shell restarts (as if they were added to .bashrc). Are there any ready utilities/scripts/whatever that can help me with that?

Ideally the uses case would look like:

aliasutil add "gcm" "git checkout master"
aliasutil add "gfp %1" "git push --force %1"
aliasutil list
aliasutil remove "gcm"
  • 2
    Aliases do not support arguments so your use of %1 is not possible, although in that specific case it would work to simply alias gfp to git push --force. Any arguments after gfp will be appended to the expansion. Shell functions can take arguments though. – camh Sep 6 '12 at 13:46
  • I knew that aliases don't have arguments, but I have described the ideal case. Didn't know about shell functions though. Thanks! – Fixpoint Sep 6 '12 at 14:27
5

alias handles creation and listing of aliases. unalias handles deleting them. if you need it, you can get more details with the bash help built-in - help alias and help unalias or in the bash man page.

To save them, I've been using the following for years:

$ alias savealias='alias > ~/.bash-aliases'
$ savealias

All aliases (including the freshly defined savealias will now be saved in ~/.bash-aliases. This is a plain text file which can be edited like a shell script if required. e.g. mine contains lines like this:

alias acs='apt-cache show'
alias acss='apt-cache search'
alias loadalias='. ~/.bash-aliases '
alias mq='mailq | grep "^[0-9A-Z]"'
alias savealias='alias >~/.bash-aliases'
alias ttystat='echo $(tty) $TERM ${COLUMNS}x$LINES'
alias where='type -all'
alias which='type -path'

and many others.

The saved aliases are loaded at login time by adding the following to ~/.bash_profile, ~/.bashrc, or even /etc/profile

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

i.e. "if a .bash-aliases file exists in my home dir, then source it."

With this method, any changes you make to aliases will be lost on logout unless you run savealias - this includes adding, editing, and deleting aliases.

5

I know of no tools that do this, however you should be able to do most of it with existing utilities. Depending on how synced you want the aliases to be you could source the list in .bashrc or prior to each command line with PROMPT_COMMAND:

ALIAS_FILE='~/.aliases'
PROMPT_COMMAND=". $ALIAS_FILE"

aliasutil add "gcm" "git checkout master"

echo 'alias gcm="git checkout master"' >> $ALIAS_FILE

aliasutil add "gfp %1" "git push --force %1"

Do you intend the %1 as an argument? Aliases do not support arguments, so to achieve this you need to create a function.

aliasutil list

cat $ALIAS_FILE

aliasutil remove "gcm"

sed -i '/^alias gcm=/d'
unalias gcm
  • The sed script for deleting needs to be sed -i '/^alias gcm=/d' – cas Sep 7 '12 at 13:59
  • you're welcome. but it still needs the = to anchor the end of the match :) otherwise it'll delete all aliases beginning with gcm, not just gcm. – cas Sep 7 '12 at 14:05
  • Indeed, I was too quick. – Thor Sep 7 '12 at 14:09
0

As I was implementing this myself (inspired by @Thor's answer), I thought I'd create a tool that others good install: https://github.com/talwrii/aliasutil.

This works for zsh and should hopefully work with bash (untested).

If you are using zsh then you can install it with zplug.

source /usr/share/zplug/init.zsh
zplug talwrii/aliasutil
zplug install
zplug load

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.