Launching Visual Studio Code from the Command Line on macOS
Visual Studio Code (VS Code) is a powerful and popular source code editor. A convenient feature is the ability to launch it from the command line, allowing you to quickly open files or projects directly from your terminal. This tutorial will guide you through the process of setting this up on macOS.
Understanding the Problem
Sometimes, simply typing code .
in your terminal doesn’t work as expected. This usually indicates that the system doesn’t recognize the code
command, meaning the VS Code executable isn’t properly registered in your shell’s PATH
. The PATH
environment variable tells your shell where to look for executable programs.
Solution: Adding VS Code to Your PATH
There are a couple of ways to fix this. We’ll cover the most common and reliable methods.
Method 1: Using the VS Code Command Palette
VS Code provides a built-in command to automatically add itself to your PATH
. This is the recommended approach as it handles the configuration for you.
- Open VS Code: Launch the Visual Studio Code application.
- Open the Command Palette: Press
⌘⇧P
(Command + Shift + P) to open the Command Palette. - Type "shell command": Start typing "shell command" in the Command Palette. You should see "Shell Command: Install ‘code’ command in PATH" appear as an option. Select it.
- Restart Your Terminal: After executing the command, you must restart your terminal (or open a new terminal window/tab) for the changes to take effect. This ensures that your shell loads the updated
PATH
variable.
Method 2: Manually Adding to Your Shell Profile
If the above method doesn’t work, or you prefer a more manual approach, you can directly modify your shell profile file. The shell profile is a script that runs every time you open a new terminal session.
-
Determine Your Shell: Most modern macOS systems use Zsh as the default shell. Older systems might still use Bash. You can check which shell you’re using by running
echo $SHELL
in your terminal. -
Edit Your Shell Profile: Open the appropriate profile file in a text editor.
- For Zsh: Edit
~/.zshrc
- For Bash: Edit
~/.bash_profile
or~/.bashrc
(.bash_profile
is usually preferred on macOS)
- For Zsh: Edit
-
Add the PATH Entry: Add the following line to the end of your shell profile file:
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
This line appends the directory containing the VS Code executable to your
PATH
. Important: Make sure the path/Applications/Visual Studio Code.app/Contents/Resources/app/bin
is correct for your VS Code installation location. If you moved VS Code, adjust the path accordingly. -
Source Your Shell Profile: After saving the changes to your shell profile, you need to tell your current terminal session to load the updated profile. Run the following command:
- For Zsh:
source ~/.zshrc
- For Bash:
source ~/.bash_profile
orsource ~/.bashrc
- For Zsh:
Now, typing code .
in your terminal should successfully launch VS Code and open the current directory.
Troubleshooting
- Permission Errors: If you encounter permission errors, it’s possible that the
code
executable doesn’t have execute permissions. You can try to fix this by runningchmod +x /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code
. - Incorrect Path: Double-check that the path to the
code
executable in your shell profile is correct. - Restart Required: Always remember to restart your terminal or source your shell profile after making changes to ensure the changes are applied.
- OSS Build: If you’re using an Open Source Software (OSS) build of VS Code, you might need to use the command
code-oss .
instead ofcode .
.