5 ms·
Why “alias” is my last resort for aliases
- deleted 2y ago[deleted]
- pwagland 2y agoThe 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.
- shellnoob 2y agonobody tell him
- JadeNB 2y ago> nobody tell him What?
- tasn 2y agoI 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.
- chime 2y agoI have alias s='source ~/.zshrc'.
- alkh 2y agoI 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
- chrisfinazzo 2y agoSourcing .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
- watusername 2y agoYes, 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!
- psychoslave 2y agoCame here to mention just this, all the more as the article mention that the author is considering a move to Fish.
- leojfc 2y agoYeah, 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). [1] https://kapeli.com/dash https://kapeli.com/dash
- BerislavLopac 2y agoI was nearly 10 years old (i.e. as a Fish user) when I learned about abbr. I used to use functions for everything. :facepalm:
- modeless 2y agoThis doesn't work for me in Ubuntu 22.04. Is it a recent feature of bash?
- mqus 2y agoThis is a feature in zsh (I don't think it works in bash and the autocomplete in bash is also a bit inferior to the zsh one)
- lvncelot 2y agoAnother 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.
- Graziano_M 2y agoOn what shell? That doesn't work in bash. I use `type` for that, since it'll tell you if it's on PATH, a function, or an alias which gdb /opt/homebrew/bin/gdb ~ type gdb gdb is aliased to `gdb -q'
- lvncelot 2y agozsh for me ~ ▸ which k k: aliased to kubectl
- pcthrowaway 2y agoIn bash this is how I get git completions working (with bash functions rather than aliases): __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/6a64ac7b014fa2cfa7a69af3c253bcd53a94b428/contrib/completion/git-completion.bash#L3958 https://github.com/git/git/blob/6a64ac7b014fa2cfa7a69af3c253...
- darrenf 2y agoThis 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. [edit] TIL: `zsh` has its own `which`.
- BenjiWiebe 2y agoSome distros use an alias which pipes all the aliases into 'which', which has a flag that supports reading aliases from stdin. Fedora does it. It's very convenient.
- saghm 2y agoI 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 ".
- inanutshellus 2y agoMy pile of aliases and custom functions is hilariously large now, making it cumbersome to find where exactly I defined one. 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.
- Honk4Stonks 2y agoI just create ~/rc_files.d/{functions,aliases,profiles,etc} which automatically get sourced with a `for` loop. This makes it easier to keep things organised.
- inanutshellus 2y agoyes, I've done basically this, but... gee is `foo` an alias or... did I upgrade it to a function? 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...
- latexr 2y ago> gee is `foo` an alias or... did I upgrade it to a function? which foo On Zsh that tells you if it’s an alias, a function, or a script.
- deleted 2y ago[deleted]
- tester457 2y agoFor more power try fish's abbreviations. With `--position anywhere` you can expand an abbreviation even if it's not at the beginning of a line.
- dmd 2y agoAnother 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).
- jacobsenscott 2y agoYes, 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.
- watusername 2y agoThis 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.
- mattgreenrocks 2y agoI 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`.
- sisk 2y agozsh also supports the "alias anywhere" concept (their term is "global alias") by using the `-g` flag. alias -g ag='2>&1 | grep' some-command ag 'words' # equivalent to: some-command 2>&1 | grep 'words'
- alkh 2y agoYeah, 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
- ale42 2y agoI'm not sure I understand this point: > 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?
- eqvinox 2y agoThere 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.
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]
- latexr 2y ago> When I create, update, or delete an alias, I have to reload my `.zshrc`. 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.
- noahjk 2y agoHere's what I use in my .zshrc with VS Code: 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.
- latexr 2y agoOn macOS, you can do that for any GUI editor by using the `open` command with the `-W` and `-n` flags. See `man open` for details.
- dcb_lu 2y agoI 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'
- kleiba 2y agoWhat about functions?
- alsoforgotmypwd 2y agoSimilar problems.
- eqvinox 2y agoLess portable and don't give the subprocess isolation that a proper script has. That's sometimes an advantage (can modify shell state — I think that's the primary one), sometimes a disadvantage (influenced by shell state [e.g. set -e], "exit" exits the entire shell, hard to do cleanup / use signal handlers, can accidentally change shell state [like cwd].) But I'd use a shell function over an alias if a script isn't an option.
- rootbear 2y agoI used to have a bunch of over-complicated aliases, but due to their limitations, I now use a mix of aliases and functions, with a few scripts in ~/bin. It’s all a matter of using the right tool. I use aliases for simple things, like default options on some commands. Things that need to happen in the current shell, but are too complex for an alias, are functions. That leaves extra-complex things that I don’t want to write a shell function for as programs in ~/bin, which can be shell, or python, or whatever.
- JamesSwift 2y agoYeah, I guess me and the author just have different opinions on it. I do the same as you: start with an alias, use a function if its more complicated, and only use scripts for things that are closer to programs in complexity/length. Using this model over the past ~10 years I have ~50/50 split of aliases/functions and only a few scripts.
- gthink 2y agoIt seems like a lot of effort to replace just `alias g=git?` Not to mention the fact that you have to move the script files across systems as well. I think the target is mostly cross-shell porting here.
- alsoforgotmypwd 2y agoSo many CLI tools delivered as bash/bourne shell scripts to source relying on aliases and other shell magic (aliases and functions rather than commands) fall apart when used in automated unattended environments like build processes.
- acatton 2y agoIn this example, it's easier to just do "ln -s /usr/bin/git ~/bin/g" instead of creating a bash script... But that's just my opinion.
- adrian_b 2y agoTrue, but I think that the author did not mean this as a real useful example. 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).
- wonger_ 2y agoThis is also a good approach for Windows since aliases on Windows don't really exist. Make a directory like C:/path/to/aliases that holds all your batch files.
- arisudesu 2y agoDoskey is a (kind of) Windows replacement for shell aliases, and it can be configured to auto-execute macros file in new cmd.exe windows.
- subjectsigma 2y agoI know you’re not talking about Powershell, but on Windows most people would be using Powershell, and that does have good alias support.
- eqvinox 2y agoPretty 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…
- arisudesu 2y agoSurely this is possible way to alias commands, but keep in mind that PATH extensions are made available not only in shell, but to all programs started from it, well, because it is environ. Whereas aliases are available only for use inside shell, but they're not executable by programs started from it. They are not the same.
- JohnKemeny 2y agoOne problem I had today with alias is that I cannot run, e.g. `timeout 10 my_alias` where `my_alias` is the alias. Does anyone know if this is possible to achieve with an alias?
- latexr 2y agoUntested for that specific command, but in Zsh this should work: timeout 10 ${aliases[my_alias]}
- GChevalier 2y agoAnother option is to create functions in the bashrc, they can contain logic like the suggested bin approach.
- oweiler 2y agoThis. Functions are almost always the better solution.
- thor_molecules 2y agoI 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.
- andoveragain 2y agoSounds 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.
- tantalor 2y ago> With an alias, I’d have to write it in Zsh. huh?
- oweiler 2y ago`alias` is shell builtin.
- sceptic123 2y agoI'm surprised they did not write a script called `alias` that creates an alias like script using the alias syntax
- subjectsigma 2y agoAt least some of these problems can be addressed by using a dotfile manager such as chezmoi or stow, specifically the conditional presence of scripts. Also, it’s perfectly possible to pass the status code of a previous program to another without builtins like $?, I’m not quite sure what he meant by that.
- Ferret7446 2y agoI think this misses the obvious. Aliases are aliases. They save typing in a shell. Actually putting them in your PATH... is kinda sketchy, as PATH is a global namespace used by everything. You almost certainly don't want exec(2) picking up the random short letter names you'd be littering in PATH.