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 articles

Ruslan Osipov
Written by author: Ruslan Osipov