Hey guys, I'm starting a   YouTube channel 🤾🏽‍♂️ please like and subscribe!

zsh: command not found: code [How to Fix]

zsh: command not found: code  [How to Fix]

Are you a macOS user using ZSH shell and have encountered the error "ZSH: command not found: code" when trying to execute a command in your terminal or shell? Don't worry, you are not alone. In this article, we will discuss what ZSH is, why this error may occur, and how to resolve it.

What is ZSH and why am I seeing this error?

Introduction to ZSH

Let's begin with a brief introduction to ZSH. Zsh (Z Shell) is a powerful shell for Unix-based operating systems like macOS, Linux, and other Unix-like systems. It is an interactive shell that provides enhancements, including programmable command-line completion, spell correction, and globbing. It also has some notable features like customizable prompts, shared command history, and a powerful scripting language.

Explanation of the error

The error message "ZSH: command not found: code" appears when you try to execute a command in your terminal or shell, and it is not recognized by ZSH shell. This error usually occurs because ZSH cannot find the executable for a particular command.

Why ZSH might be causing this error

When you install Visual Studio Code on macOS, its executable is typically added to the PATH environment variable, which allows you to execute the code command from any directory in your terminal or shell. However, if you are using ZSH shell, it may not recognize the executable path, causing the "command not found: code" error to occur.

How can I resolve the "command not found: code" error?

Check if Visual Studio Code is installed

First, ensure that Visual Studio Code is installed on your macOS system. You can do this by opening the Applications folder and checking if the "Visual Studio Code" app is present.

Make sure Visual Studio Code is added to your PATH

If Visual Studio Code is installed, the next step is to make sure it's added to your PATH environment variable. One way to do this is to run the following command:

echo $PATH

This will display the directories that are currently in your PATH. Check whether the directory containing the "code" executable is listed in the output. If not, proceed to the next step.

Manually add Visual Studio Code to your PATH

If Visual Studio Code is not added to your PATH, you can add it manually by editing your PATH environment variable. To do this, open the Terminal and enter the following command:

sudo nano ~/.zshrc

This will open the Nano text editor in the terminal, allowing you to edit your ~/.zshrc file. Add the following line at the end of the file:

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"

Save the changes made to the file by pressing control + X and then press Y to confirm the save. Finally, restart your terminal or shell for changes to take effect.

How do I install Visual Studio Code on macOS with ZSH shell?

Steps to install Visual Studio Code on macOS

If you have not installed Visual Studio Code on macOS, you can do so by following these steps:

  1. Download the Visual Studio Code installer from the official website: https://code.visualstudio.com/.
  2. Double-click on the downloaded file to start the installation process.
  3. Drag and drop the Visual Studio Code app into the Applications folder.

Adding Visual Studio Code to your PATH environment variable

After installing, you need to add Visual Studio Code to your PATH environment variable to be able to execute the code command from any directory. Follow the steps outlined above in the "Manually add Visual Studio Code to your PATH" section to add it to your PATH.

How to edit your ~/.zshrc file?

To edit your ~/.zshrc file, open the Terminal and enter the following command:

nano ~/.zshrc

This will open the Nano text editor in the terminal, allowing you to edit your ~/.zshrc file.

I have already added Visual Studio Code to PATH but I am still seeing the error, how can I fix it?

Check if the command is in your directory

Make sure that the command you are trying to execute, such as "code", is in your directory. You can check this by running the ls command in the terminal, which lists all files and directories in the current directory.

Check if the folder containing the command is in your PATH

If the command is not in your current directory, ensure that the folder containing the command is in your PATH environment variable. You can do this by running the following command:

echo $PATH

This will display the directories that are currently in your PATH. Check whether the directory containing the "code" executable is listed in the output. If not, proceed to the next step.

Export the command to your PATH environment variable

If the folder containing the executable is not in your PATH, you can add it by exporting the directory to your PATH environment variable. To do this, run the following command in the terminal:

export PATH="$PATH:/path/to/visual/studio/code/folder"

Replace "/path/to/visual/studio/code/folder" with the actual path to the folder that contains the "code" executable. Finally, restart your terminal or shell for changes to take effect.

How do I update Visual Studio Code on macOS?

Updating through the application

You can update Visual Studio Code through the application by following these steps:

  1. Open Visual Studio Code.
  2. Click "Help" in the top menu bar.
  3. Click "Check for Updates".
  4. If an update is available, follow the prompts to download and install it.
  5. Restart Visual Studio Code.

Updating through the command line

You can also update Visual Studio Code through the command line using the following command:

brew update && brew cask upgrade visual-studio-code

This will update Visual Studio Code to the latest version using Homebrew, a package manager for macOS.

Uninstalling and reinstalling Visual Studio Code

If updating does not work, or if you encounter any other issues, you can try uninstalling and reinstalling Visual Studio Code. To do this, follow these steps:

  1. Drag and drop the Visual Studio Code app from the Applications folder to the Trash.
  2. Empty the Trash.
  3. Download the Visual Studio Code installer from the official website: https://code.visualstudio.com/.
  4. Double-click on the downloaded file to start the installation process.
  5. Drag and drop the Visual Studio Code app into the Applications folder.

Related video

FAQs

What is the difference between ZSH and Bash?

ZSH and Bash are both Unix-based shells, but they have some differences. Zsh provides added functionalities and features like improved tab completion, faster startup time, and better globbing support. Zsh was created to be backward-compatible with Bash, so most Bash scripts should work in Zsh. However, some Bash commands or scripts may behave differently in Zsh.

What do I do if I still can’t fix the error?

If none of the methods above works, you can try searching online for solutions, or you can also seek the help of a data recovery professional.

How do I restart my terminal or shell?

To restart your terminal or shell, simply close and reopen it. Alternatively, you can also use the following command: 'exec $SHELL'. This will restart your current shell.

What does the error 'command not found: code' mean on a Mac?

This error usually means that the 'code' command, which is used to launch Visual Studio Code from the command line, cannot be found in your path.

What is causing the 'command not found: code' error in my terminal?

The most common cause of this error is that the necessary path environment variable has not been set up correctly or is missing. This can happen during the installation of VS Code or when switching from a different shell (e.g., Bash) to Zsh.

How can I add Visual Studio Code to my path on a Mac?

You can add Visual Studio Code to your path on a Mac by adding the following line to your .bash_profile or .zshrc file: 'export PATH=/Applications/Visual Studio Code.app/Contents/Resources/app/bin:$PATH'

Why am I getting the 'command not found: code' error even after adding VS Code to my path?

This error can occur if you are running macOS Catalina or later, which have additional security features that restrict access to certain folders. To fix this issue, you can manually add VS Code to your path using the command line.

How can I manually add Visual Studio Code to my path on a Mac?

You can manually add Visual Studio Code to your path on a Mac by creating a symbolic link in a folder that is allowed by the system, such as /usr/local/bin. To do this, run the following command in your terminal: 'sudo ln -s /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code /usr/local/bin/'

How do I resolve the 'command not found' error for Visual Studio Code on macOS manually?

To resolve the 'command not found' error for Visual Studio Code on macOS manually, you can either add VS Code to your path as described above or create an alias for the 'code' command using the command’s syntax with other examples.

How do I edit the .bash_profile or .zshrc file to add Visual Studio Code to my path?

To edit the .bash_profile or .zshrc file on a Mac, you can open your preferred text editor (such as VS Code) and navigate to the file’s location using the terminal. Once there, you can make the necessary changes to add Visual Studio Code to your path.

How can I make sure my changes to the .bash_profile or .zshrc file take effect?

To make sure your changes to the .bash_profile or .zshrc file take effect, you can either close and reopen your terminal or run the command: 'source ~/.bash_profile' or 'source ~/.zshrc'

What should I do if I make mistakes like typos when editing the .bash_profile or .zshrc file?

If you make mistakes like typos when editing the .bash_profile or .zshrc file, you can undo your changes by opening the file again and correcting the mistake. Then, save and close the file, and make sure to reload the file as described above to apply the changes.

Can I use Visual Studio Code’s terminal on a Mac?

Yes, you can use Visual Studio Code’s terminal on a Mac by opening the integrated terminal in VS Code. This allows you to run terminal commands directly from the editor and access the Mac’s terminal without leaving the editor.

Related articles

Ruslan Osipov
Author: Ruslan Osipov