I've got an alias to open files in Sublime Text 2:

alias sub='/Applications/Sublime\ Text\ 2.app/Contents/MacOS/Sublime\ Text\ 2'

But this spins up another instance of Sublime Text 2 (complete with restoring all the open files and folders in my other instance).

I would like to get files to open up as a tab in the current instance, which must be possible, since this is what right-click > Open with... > Sublime Text 2 does.

Any idea how to do that from the command line?

  • That's not how it is for me. When I call subl file1 and then subl file2, it opens a new tab for file2 next to file1. Quite the contrary, when I call open -a 'Sublime Text 2' file2, I get a new Sublime window. – slhck Jan 17 '13 at 16:27
  • Can you confirm that your alias matches mine? Are you using & to keep the cmd prompt from holding on to the instance? Also, what are you settings for the following options in SublimeText2>Preferences>Settings-Default? // OS X only: When files are opened from finder, or by dragging onto the // dock icon, this controls if a new window is created or not. "open_files_in_new_window": true, // OS X only: This controls if an empty window is created at startup or not. "create_window_at_startup": true, – Summitch Jan 17 '13 at 18:24

The reason it doesn't work is that you have the wrong alias. Here's the correct one for Sublime Text 3:

alias subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'

This is an alias that Sublime Text specifically provides for macOS. It will not block execution and—by default—open files in a tab next to the ones that are already open.

If you want to know more about why your alias didn't work, read on.

Application packages in OS X have an .app suffix, but when launched, they actually launch a binary that's specified in Info.plist in said package. This is the binary your alias links to, in the MacOS folder.

Running an application from the GUI or through Launch Services (e.g. via osascript or open) makes OS X open that binary, but at the same time ensure it's only launching one instance of that app. When you call the binary directly, you bypass this restriction and launch another instance of the application—unless the application has measures for preventing two instances from being launched, which normally OS X handles.

Also, as a side effect, launching the binary will block the terminal execution until you exit the program or suspend it to the background. Using the built-in subl from Sublime Text returns control to your terminal, as it actually uses a Launch Services call to open the Sublime package instead of addressing the binary in MacOS only.

Since you're basically launching a second Sublime Text instance, it'll show you the files it previously had opened, because that's its default behavior.

  • I'm opening multiple files using Python. How can I use this code to keep them in one window? – Daniel Springer Dec 17 '17 at 23:11
  • @DaniSpringer Just run sub *.py. – slhck Dec 18 '17 at 8:39
  • Sorry if I wasn't clear. Here's what I'm working with: github.com/DaniSpringer/readmes-opener/blob/master/file.py (it seems to open them usually in the same window if Sublime is already open/has an open window - still not sure). Where would sub *.py go in there? Or that's for a different approach? – Daniel Springer Dec 18 '17 at 13:52
  • 1
    @DaniSpringer Oh, I see. Well, from Python you could try to locate the subl executable that is part of the Sublime Text app bundle (on macOS at least) and then pass your file names as arguments, e.g. subprocess.run(["/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl", "file1.txt", "file2.txt"]). But in general I think you're overengineering your program. If you use Bash you could simply run shopt -s globstar (for recursive globs), and then subl ~/code/**/pizza*.txt. – slhck Dec 18 '17 at 16:26
  • 1
    @DaniSpringer Also, check Sublime's preferences. There's a open_files_in_new_window setting that controls startup behavior. – slhck Dec 18 '17 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.