Search code examples
environment-variableszshzsh-completion

How to setup completion of environment variable names in zsh?


In bash when I try to use autocompletion in double-quoted strings it works:

E.g.

echo "My home directory is $HO<t>"
# expands to
echo "My home directory is $HOME"

But when I try the same thing in zsh it just does nothing.

At the same time it works outside strings:

echo My\ home\ directory\ is\ $HO<t>
# expands to
echo My\ home\ directory\ is\ $HOME

Is it possible to make it work the same as bash?


Solution

  • There are two possible reasons why this doesn't work for you:

    • You need to have the following in your .zshrc file:
      autoload -Uz compinit
      compinit
      bindkey '\t' complete-word
      
      Without it, the Zsh line editor will simply expand the current expression on the line — and will try completion only if that doesn't do anything (which is why your second example does succeed).
    • Zsh completion has a bug where completion of parameter names fails if a partial parameter name is followed by a ".

    As a workaround:

    1. Make sure you're using the code from the first bullet point.
    2. When you want to complete a parameter this way, first remove the final " from your string before pressing Tab.