o_O
o_O

Reputation: 5737

ZSH not recognizing my aliases?

Using iTerm2 with zsh and it isn't recognizing my aliases. Sometimes I have to work in an IDE and can't just easily vim something and the stupid people thought it a good idea to name their applications like MyReallyLongApplicationName.app and since .html files open by default in browsers, I have to:

open -a MyReallyLongApplicationName.app something.html

I have an alias in my .zshrc like:

alias ide="open -a MyReallyLongApplicationName.app"

But zsh won't recognize my aliases. I tried another one just to see if it was me but none of the aliases I create are recognized. Just get "zsh: command not found: ide" or whatever.

So I'm not sure what I'm doing wrong and I've been searching around all day trying to fix things in zsh and the like. As a note, I'm not a pro at Linux/Unix systems so if you're too technical and vague I probably won't understand what you're telling me.

Thanks!

Upvotes: 77

Views: 111089

Answers (16)

Mobinbrf
Mobinbrf

Reputation: 1

this might sound simple but make sure to uncomment the alias you want to use in ~/.zshrc file if it's your first time using alias in zsh

open your ~/.zshrc file using a text editor (like vim) and uncomment your alias:

vim ~/.zshrc

Upvotes: 0

Aina27
Aina27

Reputation: 196

For those who still struggle , none of previous solutions work for me. I finally manage to fix this, remove additional spaces around the '=' symbol. replace this :

alias test = 'my command'

to

alias test='my command'

Upvotes: 0

json25
json25

Reputation: 161

  • Put 'source ~/.bash_profile' into ~/.zshrc file

  • Save/Exit ~/.zshrc file

  • Restart the Terminal

Upvotes: 16

Charles
Charles

Reputation: 1

I had a bad alias line that should have been obvious.

There is no error checking in these zsh custom scripts, so you may, like me, waste a couple of precious hours trying to find out why your custom aliases or functions are not loading in iTerm2.

I am using MacOS Ventura 13.1 and iTerm 2 v3.4, zsh 5.8.1.

I reloaded and using . ~/zshrc and followed a lot of the suggestions above.

I finally copied my list of aliases and function to the ~/.zshrc file. Another ~/.zshrc gave me a bad pattern error. The fix for my case was as follows. In line with a Unicode string or something that is escaped and requires ' ' (single quotes), you must use the other quote character (double quotes) to encapsulate the alias sting. In my case, I had entered.

alias fixitemerrow='printf '\e[?2004l''

The fix for this is:- alias fixitemerrow=" printf '\e[?2004l' "

Do not add spaces in your alias assignment. This is here just for illustration. Spaces would also require double encapsulation " ' ' ".

Upvotes: 0

codeskin
codeskin

Reputation: 197

What I did, in this case, was created a separate file to store all my aliases. I found this way to be cleaner and easier maintained.

My aliases file is simply called aliases and within my .zshrc I have the following:

# Linking my aliases file
source ~/foldername/aliases

Upvotes: 3

Jhamman Sharma
Jhamman Sharma

Reputation: 167

In my case issue was space b/w aliasName and equalTo. you should have to remove those space.

bad assignment

alias keu = 'k exec -it utils bash' 

correct one

alias keu='k exec -it utils bash'

Upvotes: 4

Ehsan
Ehsan

Reputation: 4291

You should put alias at the end of ~/.zshrc file. you can use below command to do that:

echo alias this='some command' >> ~/.zshrc

after that run

source ~/.zshrc

then, open a new terminal and execute the command in it.

Upvotes: 2

jturi
jturi

Reputation: 1743

I'm using both bash and zsh with one .bashrc, .bash_aliases and .zshrc file.

Put this in you .zshrc to load bash files:

# shortcut to refresh .zshrc
alias refz="source ~/.zshrc"

# Load bash files to zsh
test -f $HOME/.bashrc && . $HOME/.bashrc
test -f $HOME/.bash_aliases && . $HOME/.bash_aliases

If you have many bash aliases and functions you may will have some error messages like:

/proc/self/fd/13:12310: bad option: -t

caused by bash specific lines in.bash_aliases or .bashrc files

You can skip those problematic ones using:

if [ -n "$BASH" ] ;then
    lines to ignore by zsh
fi

For example kubectl autocompletion

# To fix error massage .bashrc:16: command not found: shopt
# Check if bash is the current shell, if not, skip it
if [ -n "$BASH" ] ;then
  # kubectl and bash completions
  if [ -x "$(command -v kubectl)" ]; then
      source <(kubectl completion bash)
      complete -F __start_kubectl k
  fi

  if ! shopt -oq posix; then
    if [ -f /etc/profile.d/bash_completion.sh ]; then
      . /etc/profile.d/bash_completion.sh
    fi
  fi
fi

# Instead I need to put this line somewhere in my zshrc 
# to have kubectl autocompletion replacing the skipped bash one:
plugins=(git git-flow brew history node npm kubectl)
# To fix error message .bash_aliases:4: parse error: condition expected: =
# Change these to this syntax to be used by zhs

# Not compatible with zsh:
if [ $HOSTNAME = "x1" ]; then

# Compatible with bash and zsh:
if [[ $HOSTNAME == "x1" ]]; then

Upvotes: 0

sunny rai
sunny rai

Reputation: 615

Need to create a profile for .zshrc and register the alias into it. (only if the profile isn't available)

cd ~ 
touch .zshrc && open .zshrc
add all the alias in .zshrc file
source ~/.zshrc

close and re-open terminal and run the alias.

Upvotes: -1

ShawnW.
ShawnW.

Reputation: 1852

if you do a very simple alias in zsh, does it work? open your .zshrc file, and add the following line:

alias ls='ls -GpF'

after adding that line, type this line in your Terminal:

source ~/.zshrc

tell us what happens. Also, just for shiggles, make sure you are using single quotes vs. double quotes, I have seen that make a difference in the past on different versions of shells/OS/whatnot.

Upvotes: 164

Mahund
Mahund

Reputation: 21

I had all my aliases on ~/.bash_profile, so i added at the last line on ~/.zshrc the following line: . ~/.bash_profile and it worked for me.

Upvotes: 2

Abram
Abram

Reputation: 41884

I needed to manually add the alias to my zsh config file and then run the source command on it.

echo alias this='some command' >> ~/.zshrc
source ~/.zshrc

Upvotes: 4

Antonio
Antonio

Reputation: 342

Make sure the double quotes are actual double quotes and not some other character which looks like double quotes.

I was editing ~/.zsh-aliases in OSX - TextEdit, which, when hitting the double quotes key substituted it for another special double quotes character, which is not what ZSH expects.

After editing the alias file with Sublime and replacing the old double quotes with actual double quotes everything runs just fine.

Hope this helps.

Upvotes: 2

Ofer Rahat
Ofer Rahat

Reputation: 868

Sometimes the simple solution is what we need... Add "source ~/.bash_profile" to your ~/.zshrc config file

echo source ~/.bash_profile >>  ~/.zshrc

Upvotes: 9

dsteplight
dsteplight

Reputation: 799

Add "source ~/.bash_profile" to your ~/.zsh config file.

Upvotes: 40

Aditya
Aditya

Reputation: 1216

After saving changes in ~/.zshrc file, open a new shell window and execute the command in it.

Upvotes: 8

Related Questions