Why “alias” is my last resort for aliases
evanhahn.com
evanhahn.com
What?
makeLinks does most of the work, then sets Homebrew's zsh as the default shell -- this currently runs in bash, so it probably will need to be updated at some point -- and everything gets reloaded.
makeLinks && chsh -s /usr/local/bin/zsh
exec $SHELL && brew doctorPersonally, I like using abbreviations ("abbr") in fish, which automatically expand as you hit space. This is especially helpful when screen sharing: No more "what does this gcob command do" questions!
Since I'm almost always on my mac, it means they're available in every shell, including remote shells, and in other situations like on Slack or writing documentation.
I mostly use § as a prefix so I don't type them accidentally (although my git shortcuts are all `gg`-consonant which is not likely to appear in real typing).
which gdb /opt/homebrew/bin/gdb
~ type gdb gdb is aliased to `gdb -q'
~ ▸ which k
k: aliased to kubectl
__git_complete grm _git_rm
grm() {
git rm "${@}"
}
Internally git uses the __git_complete function to set up completions for its subcommands, though this may be specific to your OS/distro and perhaps or git version.This does seem to ship as part of git now, here[1].
[1]: https://github.com/git/git/blob/6a64ac7b014fa2cfa7a69af3c253...
`type` is a builtin on both fish and bash though, and does resolve aliases.
[edit] TIL: `zsh` has its own `which`.
Fedora does it. It's very convenient.
> I use Bash for a lot of my scripts, but not all. For example
> I have a note-taking script called ~/bin/note which I didn’t
> want to write in Bash, so I wrote it in Python instead. With
> an alias, I’d have to write it in Zsh.
If the script is an actual script that does something more than just calling another program, then the comparison to an alias doesn't make sense. And if it's about setting up an alias to a Python program, it can definitely be done. Is there anything I'm missing here?Only if you need the change to be available immediately in the tab you’re on. Which you can ensure, appropriately, with an alias:
alias z='"${EDITOR}" ~/.zshrc && source ~/.zshrc'
Now executing `z` will open your `.zshrc` in your default editor and immediately source it afterwards. alias reload="source ~/.zshrc >/dev/null"
alias zshconfig="code --wait --new-window ~/.zshrc && reload"
the --wait flag tells zsh that it's not done executing until the window/file is closed. function edit_load() {
$EDITOR $1
source $1
if [ $? -eq 0 ]
then
echo "$1 updated."
fi
}
alias aliases="edit_load ~/.bash_aliases"
# to make sure I don't add things to $PATH on each reload
append_path () {
case ":$PATH:" in
*:"$1":*)
;;
\*)
PATH="${PATH:+$PATH:}$1"
esac
}
append_path '/blah/blah/bin'For example, if I define an alias thing, vim won't know about it.
But as a executable on my $PATH, I can do any of the following:
:%!thing
:'<'>!thing
:.!thing
A good example is on my work machine, I can't install jq because reasons. However I do have python, so I have a simple executable called fmtjson that looks like this: #!/bin/sh
python -m json.tool --indent 2
When I want to format some json from vim, I just run: :%!fmtjson
Easy to remember, easy to type. Plus I can use it in pipes on the cli.With `--position anywhere` you can expand an abbreviation even if it's not at the beginning of a line.
I have one that uses podman if it's available when I type `docker`. If I ever specifically need Docker I can use the fully-qualified path. I have a similar trick for `nvim`/`vim`.
alias -g ag='2>&1 | grep'
some-command ag 'words' # equivalent to: some-command 2>&1 | grep 'words' alias -g L="| less"
alias -g T="| tee"
alias -g C="| pbcopy"
alias -g @all="> /dev/null 2>&1"
if command -v rg > /dev/null; then
alias -g G="| rg"
else
alias -g G="| grep"
fiI think the target is mostly cross-shell porting here.
But that's just my opinion.
At least for myself, the main benefit of aliases is not to abbreviate the command name, but to change the default command-line options.
I strongly dislike the default command-line options of most UNIX utilities, e.g. cp, mv, rm and the like, so I never invoke them directly, but only through aliases that use what I consider to be the correct default options (for instance I consider that the default for any kind of copy command must be to make an exact copy of the source, instead of losing any part of the original information).
Making standalone scripts would make that trivial, so it's tempting to take him up on his idea.
That said, his list of benefits of aliases clearly makes a strong case for using them over scripts.
As he said, YMMV.
and maybe it's in neither my `functions` nor `aliases` file. Maybe it's in my `scm` file, or ... Oh!
I remember, it's in my `prompt` file because I add SCM info to my prompt and this function injects the segment of the SCM url I'm after into my PS1 variable...
which foo
On Zsh that tells you if it’s an alias, a function, or a script. timeout 10 ${aliases[my_alias]}But I'd use a shell function over an alias if a script isn't an option.
huh?