19

Similar to how to expand aliases inline in bash?

I'm using zsh 5.7.1 (x86_64-apple-darwin19.0) and Terminal 2.10 (433).

CTRL - ALT - E works for bash, what's the equivalent for zsh?

(If Use Option as Meta key is disabled in Terminal use ESC - CTRL - E)

CTRL - X - A is suggested but I can't make it work.

eethirteenzz
  • 292
  • 2
  • 7
  • 3
    `globalias` plugin for Oh My ZSH might be interesting https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/globalias at least that's what I use – Can Rau Jan 08 '20 at 17:46
  • And if you want to reveal automatically what's behind before running the command can use this: https://dev.to/equiman/reveal-the-command-behind-an-alias-with-zsh-4d96 – equiman Feb 23 '21 at 13:58

1 Answers1

33

Just press C-x a, not C-x-a (C-x is a prefix). It will call _expand_alias function to expand the alias.

Moreover, you can add this line to your zshrc then you can expand alias just with TAB:

zstyle ':completion:*' completer _expand_alias _complete _ignored

A full example zshrc:

autoload -Uz compinit; compinit;
bindkey "^Xa" _expand_alias
zstyle ':completion:*' completer _expand_alias _complete _ignored
zstyle ':completion:*' regular true
Aloxaf
  • 496
  • 4
  • 5
  • I actually did try `C-x a` I typed it wrong in my description, the result is only a system bell. Also the line in zshrc doesn't do anything, for example `alias pi="ssh root@10.0.1.2"` results in this when using TAB: `pi piconv pidpersec.d pip pip3 pic piconv5.18 ping pip2 pip3.7 pico piconv5.28 ping6 pip2.7 pis ` – eethirteenzz Jan 05 '20 at 05:23
  • 1
    @eethirteenzz I have updated to answer. If still doesn't work. Check if `_expand_alias` function is defined and `bindkey '^Xa'` gives the right output. – Aloxaf Jan 05 '20 at 07:12
  • 1
    Cheers! I was expecting zsh alias expansion to work like bash without extras, is it standard on other systems? Also, what does `autoload -Uz compinit; compinit;` actually do? Together with `bindkey "^Xa" _expand_alias` I'm set. – eethirteenzz Jan 05 '20 at 14:16
  • 1
    1. Yes. In fact, `autoload -Uz compinit; compinit;` should be enough, it will automatically binds `_expand_alias` to `C-x a`(`bindkey "^Xa" _expand_alias`). 2. It loads and calls `compinit` function to initialize zsh's completion system, including making some keybindings. – Aloxaf Jan 05 '20 at 15:32
  • 1
    You cannot have anything after the alias when you press `c-x a`. – Gqqnbig Sep 05 '21 at 07:35
  • Note also your cursor needs to be immediately after the alias . It doesn't expand the whole line, just the alias word in focus (just like regular tab completion, I'm new to non-tab completion so this was not obvious to me) – Sridhar Sarnobat Mar 07 '22 at 21:15
  • Also note you can see the existing completion rules by typing `zstyle` and pressing `` – Sridhar Sarnobat Mar 07 '22 at 21:16
  • I needed both `autoload -Uz compinit; compinit;` and `zstyle ':completion:*' completer _expand_alias _complete _ignored` for alias expansion to work. – Sridhar Sarnobat Jan 05 '23 at 00:55
  • How about adding small explanation for other three lines in the last code block? Anyway thank you for concise answer! – Roeniss Apr 25 '23 at 01:21