86
31
Is there a way to expand aliases inline in bash?
$bash>alias ll='ls -l '
$bash>ll<tab>
$bash>ls -l
86
31
Is there a way to expand aliases inline in bash?
$bash>alias ll='ls -l '
$bash>ll<tab>
$bash>ls -l
111
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.
2
This actually might be a much simpler way to do what you're trying to (bash
version >= 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
4This 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 – 2017-09-29T08:38:51.087
1
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
1are you using oh-my-zsh? – sites – 2020-02-07T22:15:19.197
That's right Should've probably mentioned it – Can – 2020-02-08T23:04:03.503
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.
4Indeed,
ESC C-e
works for Bash, butC-x a
works for Zsh. Also tested on OS X. – Blaz – 2015-10-05T19:40:46.657If you're simply looking for Ctrl+something, where something can be the "x" key, you can do it like this in some systems:
Control-x: history-and-alias-expand-line
. – igordcard – 2016-11-14T14:33:47.777Note that when you manually expand aliases that contain quotes (such as an
alias foo='this "bar"'
), the quotes will disappear upon expansion with Ctrl+Alt+E. But rest assured that they get properly sent to the actual command when you really use the alias. You can verify this by putting a--something
option inside double quotes. The command will work when used as an alias. But your program will fail with a message such as "invalid option --something" when manually expanded to the non-quoted version. – gw0 – 2016-12-15T20:48:44.440Ctrl-Alt-l
locked my X session ... I would suggest using other combinations. – Weijun Zhou – 2018-11-24T15:18:55.7971@WeijunZhou: That depends on your window manager and any custom settings you have. For Gnome, for example, lock screen is Super+L. – Paused until further notice. – 2018-11-24T15:34:59.650
@Blaz I can't get
C-x a
to work in macOS Catalina 10.15.2. – eethirteenzz – 2020-01-03T20:47:03.863Can we map alias expansion to <tab> without affecting other bash completions?. – asdfg – 2011-02-19T15:44:33.137
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 bothalias-expand-line
andcomplete
. 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. – Paused until further notice. – 2011-02-19T16:03:03.280