DEV Community

Fran C.
Fran C.

Posted on

TIL: How my complicated alias works

This post comes from this comment: You can read the whole thread here:

I think that my 2 favortites and the 2 I use more are, no doubt:

alias e='${(z)VISUAL:-${(z)EDITOR}}'
alias g='git'
Enter fullscreen mode Exit fullscreen mode

I've had this alias on my dot-files for some time. I use it to open my editor of choice (neovim).

alias e='${(z)VISUAL:-${(z)EDITOR}}'
Enter fullscreen mode Exit fullscreen mode

I didn't know what it was exactly doing until recently. I've had it copy/pasted around my dot-files many times.

EDIT: I mean... I know I always use e to open nvim. What I didn't know was why was I using such a complex alias to do it.


It uses $VISUAL or $EDITOR and makes sure that it properly handles any extra parameter given to it. So something line vim -a -b -c works.

Detailed explanation

Got it from here

What it does If I'm not mistaken is to alias e to either the value of $VISUAL or $EDITOR. But to only do that it would be enough with:

alias e='${VISUAL:-${EDITOR}}`
Enter fullscreen mode Exit fullscreen mode

There's also this (z) that I just learned on ZSH is an expansion flag. Looking into ZSH manual it says:


    Split the result of the expansion into words using shell parsing to find
    the words, i.e. taking into account any quoting in the value. Comments are
    not treated specially but as ordinary strings, similar to interactive
    shells with the INTERACTIVE_COMMENTS option unset (however, see the Z flag
    below for related options)

    Note that this is done very late, even later than the ‘(s)’ flag. So to
    access single words in the result use nested expansions as in
    ‘${${(z)foo}[2]}’. Likewise, to remove the quotes in the resulting words
    use ‘${(Q)${(z)foo}}’.
Enter fullscreen mode Exit fullscreen mode

but I still didn't know what it means... so I tried to do:

VISUAL='nvim -v' # This just writes the nvim version
alias e='${VISUAL:-${EDITOR}}'
Enter fullscreen mode Exit fullscreen mode

If I then I've tried

$ e
zsh: command not found: nvim -v
Enter fullscreen mode Exit fullscreen mode

That doesn't happen if I try with (z) flag on the expansion alias e='${(z)VISUAL:-${(z)EDITOR}}'.

Thanks to for asking the right question and for suggesting that I extracted the comment into this post.

Top comments (0)