Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
“Open in Sublime Text” in macOS context-menu

Open in Sublime Text

  • Open Automator
  • Create a new Service
  • Set “Service receives selected” to files or folders in any application
  • Add a Run Shell Script action
  • Set the script action to /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n "$@"
  • Set “Pass input” to as arguments
  • Save as Open in Sublime Text

Add to Sublime Text

Let's repeat the steps above for a second entry, this time to add files or folders to an existing Sublime Text window

  • Open Automator
  • Create a new Service
  • Add a Run Shell Script action
  • Set “Service receives selected” to files or folders in any application
  • Set the script action to /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -a "$@"
  • Set “Pass input” to as arguments
  • Save as Add to Sublime Text

Keyboard Shortcuts

You can assign a global shortcut to run the services we just created

  • Open “System Preferences”
  • Select “Keyboard” then the “Shortcuts” tab
  • In the left pane, click on “Services”
  • In the right pane, scroll to “Files and Folders”
  • Select “Open in Sublime Text” (or “Add to Sublime Text”) and click “add shortcut”
  • Select a shortcut

Edit Context Menu items

You might want to rename or edit the items we just created

  • Activate Finder
  • Click on “Finder” in the Apple menu, select “Services” then “Services Preferences”
  • In the right pane, scroll to “Files and Folders” and scroll to the item you want to edit
  • Right click the item and select “Open in Automator”
  • Edit and save

Alternatively, you can edit the workflow (e.g. ~/Library/Services/Open in Sublime Text.workflow) in your preferred text editor

@CorbMax

This comment has been minimized.

Copy link

commented Dec 14, 2017

Please fix shell scripts introducing double quotes in order to open files with space character in their name:

/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n "$@"

and

/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -a "$@"

@Orrison

This comment has been minimized.

Copy link

commented Jan 30, 2018

I am having an issue where if I have a blank instance of Sublime text open, with no project open. (as I often do, as I like to have one blank one open for working with files through FTP) regardless of having the -n option it opens the folder in that open sublime text window.

Is there any option that would make it open in a newly created window every time no matter what?

Thanks for this, by the way, it is amazing!

@chaityacshah

This comment has been minimized.

Copy link

commented Jun 19, 2018

Awesome! Thanks.

Can someone please explain "$@" in /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n "$@"

I understood that the first part finds the executable for Sublime Text, -n option is for a new window.

@partap

This comment has been minimized.

Copy link

commented Sep 4, 2018

"$@" is an automatic variable contains the input to the service.

...in this case, the filenames selected in Finder.

@johncthomas

This comment has been minimized.

Copy link

commented Nov 14, 2018

Looks like Service option has been renamed to "Quick Action" in the current version of Automator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.