t11
t11

Reputation: 87

How do you set an alias in zsh on macOS?

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

Answers (4)

Hadiuzzaman
Hadiuzzaman

Reputation: 464

Open your terminal and run the command:

  1. touch .zshrc
  2. open ~/.zshrc

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

bjc
bjc

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

t11
t11

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

Aeronautix
Aeronautix

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.

Additional notes

Reasons why your code involving aliases isn't working as expected and some additional notes to take away (speaking to zsh and bash):

  1. Aliases in a script file disappear after the script has been run (unless the script is called by another script, then the calling script has the new alias definition).
  2. Aliases executed in startup files like "~/.zshrc" etc. aren't used within script files.

Upvotes: 0

Related Questions