118

Is there a way to expand aliases inline in bash?

$bash>alias ll='ls -l '
$bash>ll<tab>
$bash>ls -l 

5 Answers 5

150

You can press Ctrl-Alt-e to perform the readline function shell-expand-line which will do alias, history and word expansions. Note that on some keyboards Meta is not Alt. You might need to press Esc then Ctrl-e

The functions alias-expand-line and history-and-alias-expand-line are not bound by default, but you can bind them by adding lines similar to the following to your ~/.inputrc file.

"\e\C-l": alias-expand-line

which would make Ctrl-Alt-l (lower case "ell") perform only alias expansion.

11
  • 2
    @asdfg: If you do, it will break other completions. It might work (untested) to create the map as shown above and then add this additional map to replace the existing one for Tab: "\C-i": "\e\C-l\e\e" which creates a macro that performs both alias-expand-line and complete. It depends on the binding from my answer above and that the default binding for Esc-Esc remains in place. You would still be able to do Esc-Esc if you wanted to do default completion. Commented Feb 19, 2011 at 16:03
  • 7
    Indeed, ESC C-e works for Bash, but C-x a works for Zsh. Also tested on OS X.
    – Blaz
    Commented Oct 5, 2015 at 19:40
  • 1
    @WeijunZhou: That depends on your window manager and any custom settings you have. For Gnome, for example, lock screen is Super+L. Commented Nov 24, 2018 at 15:34
  • 1
    It's worth adding this works only if the alias is the first part of your line, and won't work for alias in the middle of the command, e.g. it works for k get po but not for watch k get po where alias k=kubectl.
    – Meitham
    Commented May 10, 2020 at 8:29
  • 1
    @Meitham: zsh supports suffix and "global" (meaning replaced anywhere on the line) aliases as well (with alias -s and alias -g respectively), but bash does not do those. zsh.sourceforge.net/Intro/intro_8.html Commented Jun 26, 2020 at 9:32
15

For people having zsh & Oh My ZSH installed looking for a simple solution, globalias might be your friend

Expands all glob expressions, subcommands and aliases (including global).

# .zsrc:
alias S="sudo systemctl"

$ S<space>
# expands to:
$ sudo systemctl

to install just add "globalias" to you .zshrc plugin list

plugins=(... globalias)

Then just press SPACE to trigger the expansion of a command you've written.

If you only want to insert a space without expanding the command line, press CTRL+SPACE

10
  • 1
    are you using oh-my-zsh?
    – sites
    Commented Feb 7, 2020 at 22:15
  • That's right 👍Should've probably mentioned it 🤭
    – Can Rau
    Commented Feb 8, 2020 at 23:04
  • Haha I totally forgot about ctrl-space 😅
    – Can Rau
    Commented Mar 3, 2020 at 18:19
  • By the way MichaelAquilina/zsh-you-should-use is the opposite. They also work together 🤯😂
    – Can Rau
    Commented Mar 3, 2020 at 18:20
  • 1
    @CanRau Yes, it is. I just added a comment if someone wanted to use it with Zplug.
    – nvd
    Commented Dec 2, 2021 at 22:59
0

This does not work. But I'm guessing/hoping something like this can be done to do what you want to do. You would have to use your own completion script. This is how you make one:

_ll()
{
     COMPREPLY=(ls -l)
     #The next line does not work. I just hope there were a way to replace that word
     COMP_WORDS[COMP_CWORD-1]="ls -l"
}
complete -F _ll ll

Now source the full bash_completion file(http://caliban.org/bash) and put the above mentioned script in a file inside bash_completion.d directory that the script you get from the url references. Let me know if it doesn't work.

Thanks.

0

While looking into this I found this post https://stackoverflow.com/a/42699460

What stood out to me is that, according to the Bash Manual

Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a command

Testing that

eval "fn() {
  $AN_ALIAS
}"

declare -f blah

Then when I do

> alias analias='echo hello'
> alias another='analias from another alias'
> AN_ALIAS=another . ./test.sh 
fn () 
{ 
    echo hello from another alias
}

Nice, but

> alias analias='bar'
> alias another='echo foo analias'
> AN_ALIAS=another . ./test.sh
fn () 
{ 
    echo foo analias
}
foo analias

So it only expands the inner alias if it is the first. This is expected, since even if you run another in your terminal it'll print foo analias

If you really need to expand them all (and there's prob a better way to do this since this will prob not work in a lot of situations)

expand_alias() {
  local myalias="$1"
  local match=''
  match=$(
    eval "inner() {
      $myalias
    }"
    declare -f inner | sed 's/^[ \t]*//' |
      grep -vF -e 'inner ()' -e '{' -e '}'
  )

  if [[ $match != "$myalias" ]]; then
    while read -d ' ' -r item; do
      expand_alias "$item"
    done <<<"$match "
  else
    printf %s "$match "
  fi
}

expand_alias "$AN_ALIAS"

and now

> alias another && alias analias && AN_ALIAS=another . ./test.sh 
alias another='echo foo analias'
alias analias='bar'
echo foo bar 

sed to remove leading white space and grep to only include lines that do not match exactly inner (), { or }

And non of this works if you're not sourcing the script

-2

This actually might be a much simpler way to do what you're trying to (bashversion >= 4.2.29):

shopt -s direxpand
shopt -s expand_aliases

shopt's man page: https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html

2
  • 11
    This is wrong. Shell options "direxpand" and "expand_aliases" do not help expand the aliases inline like the question specifies. I do not know from which hat "direxpand" was taken...? By default, "expand_aliases" is already set. If you unset it, the result is to basically disable aliases from working (they are not expanded before interpretation of the command line). E.g. given an alias alias ll='ls -l the shell would interpret 'll' as command/function 'll' which likely does not exist.
    – FooF
    Commented Sep 29, 2017 at 8:38
  • @FooF: yeah, that option does something else: "Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt." gnu.org/software/bash/manual/html_node/Aliases.html#Aliases Commented Jun 26, 2020 at 9:22

You must log in to answer this question.

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