← Back to Index

Expand alias from bash prompt

While there exist many full fledged snippets managers with interactive prompts and fancy features, I like to achieve my goals using vanilla tools when possible.

It turns out that bash contains a very useful command, buried in the Misc page of the manual called alias-expand-line.

Add this to your .bashrc:

# "\C- " is control + space
bind '"\C- ": alias-expand-line'

# example alias
alias gca='git commit --amend'

Restart your shell and type the alias, for example here gca, then press control-space: your prompt will stay interactive and will now contain the expanded alias git commit --amend.

PS: there is also a history-and-alias-expand-line command, but I could not get it working on my setup.