"code ." is not working in on the command line for Visual Studio Code on OS X/Mac

Learn "code ." is not working in on the command line for visual studio code on os x/mac with practical examples, diagrams, and best practices. Covers macos, visual-studio-code development technique...

Fixing 'code .' Not Working for VS Code on macOS

Hero image for "code ." is not working in on the command line for Visual Studio Code on OS X/Mac

Learn how to resolve the common issue where the 'code .' command fails to launch Visual Studio Code from your macOS terminal, covering common causes and solutions.

Visual Studio Code (VS Code) is a powerful and popular code editor. One of its most convenient features is the ability to open a project or file directly from the command line using the code . command. However, many macOS users encounter an issue where this command simply doesn't work, leading to frustration and a break in workflow. This article will guide you through the common reasons for this problem and provide step-by-step solutions to get your code . command functioning correctly.

Understanding the 'code' Command

The code command isn't a native macOS command. Instead, it's a shell command that VS Code installs into your system's PATH. When you type code . in your terminal, you're essentially telling your shell to execute the VS Code application with the current directory as an argument. If this command isn't recognized, it usually means the shell can't find the code executable, or the executable itself isn't properly configured.

flowchart TD
    A[User types 'code .' in Terminal] --> B{Is 'code' in PATH?}
    B -- No --> C[Command Not Found Error]
    B -- Yes --> D{Is 'code' executable linked correctly?}
    D -- No --> E[VS Code doesn't launch or error]
    D -- Yes --> F[VS Code launches with current directory]

Flowchart of the 'code .' command execution process.

The Primary Solution: Installing the Shell Command

The most common reason for code . not working is that the VS Code shell command hasn't been installed or has been removed. VS Code provides a built-in way to install this command directly from the application.

1. Open Visual Studio Code

Launch Visual Studio Code application from your Applications folder or Spotlight search.

2. Access the Command Palette

Once VS Code is open, press Cmd + Shift + P (or F1) to open the Command Palette.

3. Install 'Shell Command'

In the Command Palette, type shell command and select the option Shell Command: Install 'code' command in PATH. You might be prompted for your administrator password.

4. Restart Your Terminal

Close all open terminal windows and open a new one. This ensures that your shell reloads its PATH environment variable.

5. Test the Command

Navigate to a project directory in your new terminal window and type code . to verify it's working.

Troubleshooting Advanced Scenarios

If the primary solution doesn't work, there might be other factors at play, such as issues with your shell configuration or permissions.

1. Verify PATH Configuration

The code command relies on a symbolic link being created in /usr/local/bin. This directory should be part of your shell's PATH. You can check your PATH by running:

echo $PATH

Checking your shell's PATH environment variable.

Look for /usr/local/bin in the output. If it's missing, you'll need to add it to your shell's configuration file (e.g., .zshrc, .bash_profile, or .profile).

# For Zsh (default in modern macOS)
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

# For Bash
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

Adding /usr/local/bin to your PATH for Zsh or Bash.

You can manually verify if the symbolic link for code exists and points to the correct location.

ls -l /usr/local/bin/code

Checking the symbolic link for the 'code' command.

The output should look something like this: lrwxr-xr-x 1 root wheel 66 Jan 1 12:00 /usr/local/bin/code -> /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code

If the link is broken or points to a non-existent location, you might need to manually recreate it or re-run the VS Code shell command installation.

3. Permissions Issues

Occasionally, incorrect permissions can prevent the code command from executing. While less common for /usr/local/bin, it's worth checking if other solutions fail.

sudo chmod +x /usr/local/bin/code

Ensuring the 'code' symbolic link is executable.