Set VS Code as the default editor on macOS

To make VS Code the default editor on macOS, use duti for Finder file associations, install the official code command in your shell, and set Git's core.editor to code --wait.
I'll first describe how to configure vs code as the editor when you open a file in Finder. The second section will describe how to set code as the editor in shell and git.
Set VS Code as the default editor in macOS Finder
- First you should find the name of your vscode editor. Use this shell command.
osascript -e 'id of app "Visual Studio Code"'
This will probably return com.microsoft.VSCode. Use the command output on your Mac in the script below if it's different.
- You will use
dutito change the default handler for a file type on mac. Installdutiwith brew or your package manager.
brew install duti
- To set all text files and code files to vscode instead of xcode use this set of shell commands.
duti -s com.microsoft.VSCode public.json all
duti -s com.microsoft.VSCode public.plain-text all
duti -s com.microsoft.VSCode public.python-script all
duti -s com.microsoft.VSCode public.shell-script all
duti -s com.microsoft.VSCode public.source-code all
duti -s com.microsoft.VSCode public.text all
duti -s com.microsoft.VSCode public.unix-executable all
# this works for files without a filename extension
duti -s com.microsoft.VSCode public.data all
duti -s com.microsoft.VSCode .c all
duti -s com.microsoft.VSCode .cpp all
duti -s com.microsoft.VSCode .cs all
duti -s com.microsoft.VSCode .css all
duti -s com.microsoft.VSCode .go all
duti -s com.microsoft.VSCode .java all
duti -s com.microsoft.VSCode .js all
duti -s com.microsoft.VSCode .sass all
duti -s com.microsoft.VSCode .scss all
duti -s com.microsoft.VSCode .less all
duti -s com.microsoft.VSCode .vue all
duti -s com.microsoft.VSCode .cfg all
duti -s com.microsoft.VSCode .json all
duti -s com.microsoft.VSCode .jsx all
duti -s com.microsoft.VSCode .log all
duti -s com.microsoft.VSCode .lua all
duti -s com.microsoft.VSCode .md all
duti -s com.microsoft.VSCode .php all
duti -s com.microsoft.VSCode .pl all
duti -s com.microsoft.VSCode .py all
duti -s com.microsoft.VSCode .rb all
duti -s com.microsoft.VSCode .ts all
duti -s com.microsoft.VSCode .tsx all
duti -s com.microsoft.VSCode .txt all
duti -s com.microsoft.VSCode .conf all
duti -s com.microsoft.VSCode .yaml all
duti -s com.microsoft.VSCode .yml all
duti -s com.microsoft.VSCode .toml all
Set VS Code as the shell and Git editor
-
Install
codeon your command line. Launch VS Code, open the Command Palette withCmd+Shift+P, and run Shell Command: Install 'code' command in PATH. Restart the terminal afterwards. This is the method in the current VS Code macOS setup guide. -
Set these environment variables in your shell configuration, usually
~/.zshrcor~/.bashrc.
export EDITOR="code"
export VISUAL="code"
- Now set the git config to use vscode
git config --global core.editor "code --wait"
Summary
That's it! You should be able to open any of those kinds of files with VSCode on your Mac now.
Update 29 Jan 2022 - I added this and more to comprehensive instructions for configuring a modern developer shell and MAcOS in another article here.
You can save hours of time settings up a new machine by purchasing my full set developer environment setup scripts with this and a lot more built-in - https://darraghoriordan.gumroad.com/l/devshell.