macosterminalvisual-studio-code

Run / Open VSCode from Mac Terminal


I'd like to run / open Visual Studio Code from the Mac OSX Terminal by running this command code .. I found instructions here:

https://code.visualstudio.com/Docs/setup

Apparently I need to include this in my .bashrc file, so I did, but to no avail.

code () {
    if [[ $# = 0 ]]
    then
        open -a "Visual Studio Code"
    else
        [[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
        open -a "Visual Studio Code" --args "$F"
    fi
}

I edited the .bashrc file here:

~/.bashrc which points to /Users/username/.bashrc

Which .bashrc should I be editing?


Solution

  • According to the docs on Launching from the command line:

    1. Open Visual Studio Code
    2. Open the command pallette with Command + Shift + P (or F1)
    3. Type Shell in command palette
    4. Select Shell Command: Install code in PATH from suggested list

    Open VSCode via Terminal/Command Prompt

    That's it.

    Now open your terminal type.

    $ code .
    

    To make this change persist after restart on MacOS

    Many Mac users find this is forgotten and needs to be re-applied after any restart. This may happen if MacOS has applied the quarantine attribute to VS Code, which the OS uses for the "Are you sure?" notice applied on first using apps downloaded from the internet.

    To check if this attribute is applied, look for com.apple.quarantine in the list returned by this command (changing the path if that's not where you installed it):

    xattr "/Applications/Visual Studio Code.app"
    

    If that does return com.apple.quarantine, you can remove the attribute using the same command with the -d flag (alongside -r to recursively remove it from all contained files and sudo to allow the change):

    sudo xattr -r -d com.apple.quarantine "/Applications/Visual Studio Code.app"
    

    ...then do Shell Command : Install code in PATH as above after the attribute has been removed, and it should persist after restart.

    Credit: derflounder.wordpress.com article linked to by RicardoVallejo in this comment.