Ryan
Ryan

Reputation: 24073

Make command line aliases reveal the true command (Mac)

I've already created a bunch of aliases in a file that my ~/.zshrc imports, such as:

alias sl='git status -uall'

When I run my aliases (such as sl) in Terminal, they work as expected.

However, I'd love if before running their command they would print the commmand itself that they're about to run.

E.g. when I type sl and press Return, it would be great if I then saw in the Terminal git status -uall and then the output of that command.

That way, I'd continually be reminding myself of what my aliases stand for.

How could I do this?

Upvotes: 3

Views: 849

Answers (1)

chepner
chepner

Reputation: 531818

One idea would be to just add a call to alias to the start of your definition:

$ alias foo='alias foo; echo bar'
$ foo
alias foo='alias foo; echo bar'
bar

The closest bash has to automatically printing the value of an alias is to use the xtrace option (though that outputs every command to standard error as it is executed).

You can also bind the Readline command alias-expand-line to some key.

bind '"\C-x\C-a": alias-expand-line'

Now typing Control-x, Control-a sometime before typing Enter will cause bash to perform alias expansion on the current command line in-place, letting you see, edit, and/or abort the resulting command before executing it.

Upvotes: 5

Related Questions