Fixing “command not found: code” on Mac: A Guide for Visual Studio Code Users
Encountering the zsh: command not found: code
error can be a frustrating roadblock for developers and coders eager to streamline their workflow using Visual Studio Code (VS Code) directly from their Mac's terminal. This error pops up when the system fails to recognize the code
command, a shortcut to open VS Code right from the command line. If you're stuck with this error, fear not! This guide is here to walk you through the steps to get you back on track, allowing you to open VS Code with just a simple terminal command.
Step 1: Confirm Visual Studio Code Installation
First things first, ensure that Visual Studio Code is indeed installed on your Mac. It might sound elementary, but sometimes the simplest oversight is the root of the issue. If you haven’t installed VS Code yet or are unsure, head over to the official website and download it. Installation is straightforward: just drag the VS Code application into your Applications folder.
Step 2: Install Command Line Tools
With VS Code installed, the next step is to make sure its command line tools are integrated into your system. This is what allows you to use the magical code
command from anywhere in your terminal.
- Open Visual Studio Code.
- Bring up the Command Palette by pressing
Cmd + Shift + P
. - Type and select
Shell Command: Install 'code' command in PATH
.
This action extends an invitation to the code
command to join your system's PATH, making it globally accessible from your terminal.
Step 3: Restart Your Terminal
Changes in the PATH often require a fresh terminal session to take effect. So, close your terminal and reopen it, or simply open a new tab or window. This step ensures your system acknowledges the recent change and is ready to recognize the code
command.
Step 4: Test the code
Command
It’s moment of truth time. Navigate to any directory in your terminal and type code .
to instruct VS Code to open with the context of the current directory. If everything is set up correctly, you should see VS Code spring to life, ready for action.
Manual PATH Addition: A Plan B
If, for some reason, the above steps don’t resolve the issue, there’s a manual workaround to add VS Code to your PATH:
- Open Terminal.
- Edit your Z shell configuration file by typing
open ~/.zshrc
. (Bash users, your file is~/.bash_profile
.) - Add the line
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
to the file. - Save the file and exit the editor.
- Apply the changes with
source ~/.zshrc
(orsource ~/.bash_profile
for Bash).
This manual method ensures the code
command knows exactly where to find VS Code, clearing the path for a hassle-free coding session directly from your terminal.
Wrapping Up
With these steps, the zsh: command not found: code
error should now be a thing of the past, letting you launch Visual Studio Code from your terminal with ease. This not only saves time but also integrates VS Code more deeply into your development workflow on Mac. Should you encounter any bumps along the way, remember that the developer community is vast and supportive. Don't hesitate to seek out additional help or clarification.
Happy coding!
If you like you can follow me on Twitter: https://twitter.com/zouyu1121