The other advantage that the article misses for aliases is that completions will work through aliases. So, in the case give for `alias g=git` typing `g <tab>` will give you the bash/zsh completions for git. You _can_ do this for the script as well, but then you need to code that up separately.
Yes, and this is why I recommend against the alias-y wrappers.
Personally, 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!
Yeah, I do something kind of similar, using Dash [1] snippets which expand to full commands.
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).
Another advantage of using alias: `which [alias]` actually tells you what that alias is mapped to, and not just the location of a script in your bin folder.
This is something I've never seen. `which` (at least on the hosts I currently have access to) is its own binary `/usr/bin/which` and unrelated to the shell and its aliases or abbreviations.
`type` is a builtin on both fish and bash though, and does resolve aliases.
I remember years ago seeing a trick related to this to allow aliases after `sudo` (which I think by default won't recognize them due to running as a separate user) by doing `alias sudo="sudo ".
I came here to say the same thing.
I'm also not convinced about the instant reload aspect. You can always just define your aliases in a different file and source it when updating aliases (how often does that happen anyway?), source your .zshrc, or just open a new terminal window.
Sourcing .zshrc works reasonably well. I have the following at the end of setup.sh -- which creates symlinks and sets up other configurations.
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 doctor
I have an alias reload="exec zsh" for fast reloading. Admittedly, this does wreck up everything set up locally but I doubt you will be modifying ~/.[zsh|bash]rc file often
> 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?
There isn't a well-defined / binary boundary to an "actual script"; I'm assuming this "note" thing is something like a 10-liner in Python. It's simply two curves of scripting in sh/bash/zsh becoming more difficult vs. Python starting with more effort but a more shallow curve — at some point the curves cross. But that crossover point is different for each person, and the other option doesn't immediately become impossible.
I do something similar to my .bash_aliases, but with a few short additions to make sure running it multiple times doesn't have any strange side effects. For instance:
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'
I reach for the ~/bin/thing approach when I want the utility to be useable from vim.
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.
Another great advantage of fish abbreviations is your history shows the actual command that was run (and in fact as soon as you've typed it, it expands, so you SEE what you're running).
Yes, essentially on the fly editable aliases. Fish is so good. I assume there's a zsh plugin that mimics that functionality - zsh people should try it.
I like putting my "aliases" into the functions directory of my config.
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`.
This is the right way. I mentioned in another comment, but abbreviations also shine when screen sharing: Everyone can follow along quickly without having to ask the "what does this gcob command do" questions.
Yeah, it's very convenient. Here are a few of mine(tested on OS X):
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"
fi
Sounds like overengineering. If you just need a shortcut for a command, that's exactly what aliases are for. If you need something more sophisticated, you probably are going to lean to scripts or functions.
Pretty much agree with most. I am wondering about paths and locations; there's now ~/.local/bin which is where e.g. a user-wide "pip install" would put things [when used without a venv]… ~/bin is not such a "standardized" location. I kinda started using both and am a bit torn now…
Personally, 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).
[1] https://kapeli.com/dash
which gdb /opt/homebrew/bin/gdb
~ type gdb gdb is aliased to `gdb -q'
`type` is a builtin on both fish and bash though, and does resolve aliases.
[edit] TIL: `zsh` has its own `which`.
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.
What?
Deleted Comment
Only if you need the change to be available immediately in the tab you’re on. Which you can ensure, appropriately, with an alias:
Now executing `z` will open your `.zshrc` in your default editor and immediately source it afterwards.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:
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: When I want to format some json from vim, I just run: 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`.
I think the target is mostly cross-shell porting here.