DEV Community

Discussion on: What are your preferred bash aliases?

Collapse
 
nflamel profile image
Fran C.

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

alias e='${(z)VISUAL:-${(z)EDITOR}}'
alias g='git'
Collapse
 
nocnica profile image
Nočnica Mellifera

Wait, what is that first one foist?

Collapse
 
nflamel profile image
Fran C. • Edited

I didn't know what it was doing until you asked. I've had it copy/pasted around my dot-files many times. I've just realized now that it does not work on BASH, only on ZSH, sorry :(

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

TL;DR:

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

Detailed explanation

Got it from here github.com/sorin-ionescu/prezto/bl...

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}}`

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

z

    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}}’.

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}}'

If I then I've tried

$ e
zsh: command not found: nvim -v

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

PS: Thanks a lot for making me look it up :)

Thread Thread
 
nocnica profile image
Nočnica Mellifera

Please write this up into an article or I will, it’s great! And since OS X switched from Bash to zsh I Can actually use it!

Thread Thread
 
nflamel profile image
Fran C.

Done! dev.to/nflamel/til-how-my-complica...

I'll try to see if I have more things like that around my dot-files to see which ones are worthy of some more articles.