9

As a terminal noob, I've created aliases for pretty much everything I do. The problem is that I've started forgetting those few commands that I do know because of it. On top of that, I sometimes need to edit a variable in the previous command.

So what I'd like is if when I use an alias, the first line printed is the actual command it represents, then proceed to execute the command. Since pressing up and !! simply reprints the alias, I'm not too sure how to get a reference to the underlying command.

Thanks.

willlma
  • 7,353
  • 2
  • 30
  • 45
  • 1
    But do you know that if you write `alias your_alias` this alias gets printed? – fedorqui Mar 24 '14 at 14:46
  • Ha! That'll do it. I'll just make alias functions then (or whatever they're called) and make that the first line. If you think that'll work, then feel free to post it as an answer. – willlma Mar 24 '14 at 14:47

3 Answers3

13

You can always use:

  • alias to list all your aliases, or
  • alias name to show the specification of the alias name.

So in fact you can define your alias as

alias myalias="alias myalias; <do stuff>"

I also think that chepner's answer with Alt-Control-e is more practical, but I am posting this for completeness.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
10

After you type your alias, but before you hit enter, you can type Meta-Control-e (probably Alt-Control-e, but possibly Esc-Control-e) to expand what you've typed; this will expand any aliases and history expansions so you can see the "long" form of what you've typed.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • I have this in profile: **alias es2="ember server -p 4202“** but when I expand it I only get **ember** – Nikos Feb 24 '15 at 14:34
0

You can use variables in your alias to get a clean output:

alias test='_TMP="pushd /home/a/b/c";echo $_TMP; eval $_TMP'

If you want to avoid messages coming out of alias, you can do

_TMP="pushd /home/a/b/c >/dev/null 2>&1"

It does add an environment var of _TMP.

The old post had error in it.

  • Happy to find this post but the suggestion is not working for me. Does it matter that I'm using zsh on Mac? What I get back in the terminal windows after inserting `test='$_TMP="php artisan serve";echo $_TMP;$_TMP'` into my .zshrc file is `zsh: php artisan serve not found`. I was expecting to see `php artisan serve` followed by the server booting up. Any guidance appreciated. – mann Nov 13 '21 at 21:54