ZSH aliases not found - shell

When I open my ~/.zshrc file and add alias homestead=“cd ~/Homestead”, I expect to be able to type homestead and be taken to the Homestead folder.
Instead I get the following error:
zsh: command not found: “cd
Even when I use single quotes, i.e. alias homestead='cd ~/Homestead' and run source ~/.zshrc I get the same error.
UPDATE: Also, when I run which homestead I get homestead: aliased to "cd
How can I fix this?

The answer was to open ~/.zshrc in Sublime Text as opposed to TextEdit and to check that the " were coming up as 042 in an octal dump.

You don't need to define this alias at all in zsh. Add the following to your .zshrc:
setopt autocd
cdpath+=(~)
The first allows you to treat a directory name as a command, which implicitly sets the working directory of the current shell to the named directory. The second specifies that if the current directory doesn't have a directory whose name is used with cd (or by itself with autocd set), then try to find it in a directory named in the cdpath parameter.
With these two, simply typing Homestead will first try to run a command named Homestead; failing that, it tries to cd to ./Homestead, and failing that, will finally succeed in cding to ~/Homestead.

The double quotes must be ASCII, not Unicode outside the ASCII range. Load the file in your editor, disable any automatic mangling of single quotes and double quotes. Then replace the funny quotes with ASCII quotes " (code decimal 34, hex 22, octal 042). Or type the command at a prompt, then cut & paste it in your editor. If all else fails, add the alias at the end of your .zshrc with
printf 'alias homestead="cd ~/homestead"' >> ~/.zshrc
Verify the result with octal dump,
od -bc .zshrc
The number above the quotes should appear as 042.

Maybe your locale settings is auto correcting a double quote " into a localized double quote “ as you posted. Since this is not recognized as a valid quote in shell, a simple white-space would break the string. So the actual alias is “cd.
As to why alias homestead='cd ~/Homestead' does not work, it seems you changed the alias in ~/.zshrc. From the which homestead result, it can be seen that alias homestead='cd ~/Homestead' does not really work. Maybe there is another line of alias homestead=“cd ~/Homestead” hidden in .zshrc after it.

Just saying that for me what fixed it was an error in the first alias in my list that had a question mark in it.
Just switched to Mac OS Catalina and ~/.bashrc to ~/.zshrc and I guess zsh doesn't support question marks.
Maybe it'll help someone coming here from Google search like I did.

Related

opening Sublime text from Git Bash but getting comamand not found

i messed up a little bit in my GIt Bash, I added an alias and can't get rid of it. whenever l launce Git Bash I get this error right away:
bash: alias: alias: not found
and when I'm trying to run subl I am getting this error message:
bash: C:Program: command not found
even though it is added in my environment variables, however, I am able to open it in CMD. could be the alias is causing the issue? if that's the case how can I get rid of it?
That error looks like you have the alias path defined incorrectly.
Your alias for subl also looks like it is using a Windows path. The examples below are from my dev machine for my GitExtension and Powershell aliases on a Windows machine.
Take note how it is using a unix style path wrapped in single quotes and without a space between the equal sign and the first single quote mark.
alias ps='"/c/windows/system32/windowspowershell/v1.0/powershell.exe"'
alias gite='"/c/Program Files (x86)/GitExtensions/GitExtensions.exe"'
Comment out the alias that you are having problems with to troubleshoot this alias issue. The aliases are typically defined in the .gitconfig file. Mine is located at c:\Users\nbstrat\.gitconfig.
From the path you specified in the comments above, updated your subl alias path to this:
alias subl='"/C/Program Files/Sublime Text 3/sublime_text.exe"'
Close your current git bash shell and then restart git bash so it will reload the updated aliases.

Editing ~/.bash_profile doesn't customize command-line prompt properly

Related Issue: How to change the terminal prompt to just current directory?
I have added export PS1="\w\$ " to my ~/.bash_profile, but the prompt for the command line just displays this:
\w$
It recognizes the backslash escape for the $ character, but not for the filepath. It does the same thing when I use a capital 'W'.
The problem was that my terminal is running zsh. I created ~/.zshrc and wrote the following code:
PROMPT="%/\$ "
I referred to this resource to find the appropriate characters for displaying the current file path: ZSH Prompt Expansion.

.bash_profile aliases: command not found

I cannot get my .bash_profile aliases to work on my Mac OSX Terminal. I created a .bash_profile file in my ~/ directory, then wrote two lines:
echo bash profile has loaded
alias prof=“open ~/.bash_profile”
I saved and entered in Terminal command:
. ~/.bash_profile
Terminal displayed:
bash profile has loaded
-bash: alias: /Users/kennethlarose/.bash_profile”: not found
I've been reading up on alias profiles, and I believe my syntax is valid. I know the profile is sourcing because it displays the echo, but Terminal will show the same 'not found' message no matter what command I save in the alias. Does anybody know what else I can try?
Let's ask shellcheck!
In .bash_profile line 2:
alias prof=“open ~/.bash_profile”
^-- SC1015: This is a unicode double quote. Delete and retype it.
There's your problem. OS X has turned your double quotes into fancy slanted quotes that bash doesn't recognize. If you're programming, you may want to disable "smart quotes".

zsh with iterm 2 does not recognize my alias

When I add an alias to ~/.zshrc such as alias dir='cd ~/Desktop/somedir' and do source ~/.zshrc, it says "command not recognized 'cd " when trying to use the alias in iterm 2.
However if I create the alias inside of iTerm 2 and not the ~/.zshrc file, it does recognize it. Anyone knows why this happens?
Thanks!
It seems that the single-quotes in your ~/.zshrc are not recognized as such and thus zsh assumes that two aliases are given: The definition of dir='cd and a query of ~/Desktop/somedir' instead of dir='cd ~/Desktop/somedir'. (zsh allows for multiple alias definitons and queries in one command)
Most likely the single quotes you are using are not ' from ASCII (ASCII code 0x27) but some Unicode representation. Depending on your editor it is possible that they got replaced automatically when you edited your ~/.zshrc. To be sure use an text editor that doesn't do any such 'auto-magic' (unless you tell it). I think of nano, vim and/or emacs are available on recent OS X versions.

How to suppress (or customize) Mac Terminal shell prompt

Currently in my Terminal, every shell prompt looks like ComputerName: FooDir UserName$. The UserName part simply wastes too much space out of my precious 80 columns. Is there a way to suppress it?
The prompt is defined by the environment variable PS1 which you can define in .bash_profile.
To edit it, open or create the (hidden) file .bash_profile:
nano .bash_profile
and add a line that says
export PS1=""
Between the quotation marks, you can insert what you would like as your terminal prompt. You can also use variables there:
\d – date
\t – time
\h – hostname
\# – command number
\u – username
\W – current directory (e.g.: Desktop)
\w – current directory path (e.g.: /Users/Admin/Desktop)
The default prompt for common Linux distributions would be \w $, which evaluates to ~ $ in your home directory or e.g. /Users $ somewhere else. There are also website (like this one) that can help you with building your prompt.
If you want to remove the UserName part, your choice would be \h: \w$.
Once you made your changes, save the file with Control+o, Return, Control+x.
Here's an excellent article with a full list of Variables and Colors:
Customize your Shell Command Prompt
For a simple, minimalistic prompt, you can try this. Add the following line to your .bash_profile or simply test it first by running it in your terminal:
export PS1="\[\033[0m\]\w\$ "
It'll look something like this:
Here's my Prompt (source), also very simple:
export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "
2019 onwards, MacOS default shell is Z Shell. To customize command prompt, add a file named .zshrc in user home and put following line that sets a PS1 environment variable with desired prompt format:
export PS1="[%n]%~> "
Open new terminal
This is result of following format expansion:
%n User name
%~ Current directory
See full list of available expansions here.
Your answer can be found right here:http://www.hypexr.org/bash_tutorial.php#vi at about the middle of the page. :)

Resources