Reputation: 87
I've set a zsh alias like this:
alias sed="/usr/local/Cellar/gnu-sed/4.8/bin/gsed"
I can confirm it is working by running:
type sed
sed is an alias for /usr/local/Cellar/gnu-sed/4.8/bin/gsed
However, if I put exactly the same code, alias setting and type sed
, then in a script under the filename test and run it get the default sed
:
zsh test
sed is /usr/bin/sed
I've also tried it with extending PATH
and still get the same thing which puzzles me.
Upvotes: 5
Views: 26004
Reputation: 464
Open your terminal and run the command:
Paste your alias on the editor like below - and save it.
alias runner="dart run build_runner build --delete-conflicting-outputs"
alias dev="flutter run --flavor dev -t lib/main_development.dart"
If the file already has other things in it, that's fine. You can put it at the end.
Upvotes: 17
Reputation: 1369
To answer the question in the title, "How do you set an alias in zsh on Mac OS," the syntax in the question is correct. For example
alias gst="git status"
As you would expect, now when I enter gst
, it runs git status
The reason it didn't work with the test
script is because of how it was run. The zsh test
command creates a new zsh
shell, and aliases do not transfer to new shells/sub-shells, unlike environment variables. Functions also do not transfer to new shells, so that doesn't help.
If you source the script file instead, it will run the commands in the file in the current shell instead of creating a new one. Then the sed
alias will work. So, instead of zsh test
, do this
source test
The common solution is Hadiuzzaman's answer: put alias
commands in ~/.zshrc
. Whenever, a new z-shell is created, using zsh
for example, the commands in ~/.zshrc
are executed in the newly created shell, so your aliases will always be available in z-shells. But, if you want them in bash shells, too, you need to put them in ~/.bashrc
. Since the syntax for aliases are the same for bash
and zsh
, I put mine in a separate file, ~/.aliases
, and then put source .aliases
in my ~/.zshrc
and ~/.bashrc
files. That way I only need to update one file instead of two.
Upvotes: 2
Reputation: 87
The only thing that worked for me was to create a function. After that sed was overriden with gsed when I called the help argument
#!/bin/bash
sed () {
/usr/local/Cellar/gnu-sed/4.8/bin/gsed "$@"
}
sed --help
Upvotes: 0
Reputation: 316
If you want to use the command word sed
as gsed
in all your future scripts (which if you really want to do, you can do this), Homebrew has info on how to achieve this, at the end of the installation for brew install gsed
:
GNU "sed" has been installed as "gsed".
If you need to use it as "sed", you can add a "gnubin" directory
to your PATH from your bashrc like:
PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
Though you are asking about zsh
so I'm assuming you've upgraded to at least macOS Catalina and are now trying to use zsh
instead of bash
, so that means I would suggest putting the suggested line from Homebrew into ~/.zshrc
instead of ~/.bashrc
.
Some things have changed, the default Homebrew installation directory is now, well you can use the command brew --prefix
to get your directory, it's now currently /opt/homebrew
instead of the old /usr/local/Cellar
.
Now your sed
should run as gsed
in all your scripts.
Reasons why your code involving aliases isn't working as expected and some additional notes to take away (speaking to zsh
and bash
):
Upvotes: 0