Set vscode as the default editor for text files on mac

Photo by Markus Spiske on Unsplash

If you use VSCode you might want to set it the default editor for all text and code files on your mac.

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.

Setting vscode as the default editor for text files in Mac Finder

  1. 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.

  1. You will use duti to change the default handler for a file type on mac. Install duti with brew or your package manager.
brew install duti
  1. 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

Setting vs code as the default editor in shell and git

  1. The first step is to install code to your command line. From the Visual studio code documentation: "Launch VS Code. Open the Command Palette (Cmd+Shift+P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command."

  2. Set these env vars in the rc file for your shell. This might be ~/bash-profile or ~/zshrc for example.

export EDITOR="code"
export VISUAL="code"
  1. Now set the git config to use vscode
git config --global core.editor "code -n --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://usemiller.dev/dev-shell.