Paweł Bajorek

Bajorek

Blog

Set Your Default Code Editor for All Source Files on macOS

The Problem

If you know the pain when you double-click a .json file and macOS opens it in TextEdit or Xcode. Every. Single. Time.

You can right-click → "Open With" → choose your editor, but that's tedious. You can change individual file associations in Finder's "Get Info" panel, but doing that for dozens of file types is painful.

The Solution

Use duti - a command-line tool for managing file associations on macOS.

1. Install duti

brew install duti

2. Get Your Editor's Bundle ID

osascript -e 'id of app "Visual Studio Code"'
# com.microsoft.VSCode

For other editors:

  • Cursor: osascript -e 'id of app "Cursor"'
  • Sublime Text: osascript -e 'id of app "Sublime Text"'
  • Zed: osascript -e 'id of app "Zed"'

3. Set Associations for All Code Files

Here's a script that covers most programming languages and config files:

editor_id=$(osascript -e 'id of app "Visual Studio Code"')

extensions=(
  c h cpp hpp
  py ipynb ini
  js ts jsx tsx
  rb erb gemfile gemspec rakefile rake rdoc spec
  ex eex ejs erl
  java go rs ru php
  tf tpl tmpl
  css scss sass svg
  json yaml yml xml toml csv
  md mdx txt log
  sh zsh bash profile zshrc
  sql
  env example sample
  gitconfig gitignore
)

for ext in "${extensions[@]}"; do
  duti -s "$editor_id" ".$ext" all
done

Add more extensions as needed - the pattern is simple.

4. Handle Executable Scripts

Some files don't have extensions but are identified by their UTI (Uniform Type Identifier). Set those too:

executables=(
  public.shell-script
  public.python-script
  public.ruby-script
  public.perl-script
  public.unix-executable
  public.script
  com.netscape.javascript-source
  com.apple.applescript.text
)

for exe in "${executables[@]}"; do
  duti -s "$editor_id" "$exe" all
done

5. Verify It Worked

duti -x .py
# Should show your editor

Full Script

Save this as set-editor.sh and run it whenever you set up a new Mac:

#!/bin/bash

brew install duti

editor_id=$(osascript -e 'id of app "Visual Studio Code"')

extensions=(
  c h cpp hpp
  py ipynb ini
  js ts jsx tsx
  rb erb gemfile gemspec rakefile rake rdoc spec
  ex eex ejs erl
  java go rs ru php
  tf tpl tmpl
  css scss sass svg
  json yaml yml xml toml csv
  md mdx txt log
  sh zsh bash profile zshrc
  sql
  env example sample
  gitconfig gitignore
)

for ext in "${extensions[@]}"; do
  duti -s "$editor_id" ".$ext" all
done

executables=(
  public.shell-script
  public.python-script
  public.ruby-script
  public.perl-script
  public.unix-executable
  public.script
  com.netscape.javascript-source
  com.apple.applescript.text
)

for exe in "${executables[@]}"; do
  duti -s "$editor_id" "$exe" all
done

echo "Done. All source files now open in $editor_id"

Summary

Save the script. After a macOS update or a reinstall of Xcode / xcode-select, the default application may change. When that happens, run this once to restore your preferred editor.