Skip to content

Instantly share code, notes, and snippets.

@byBretema
Forked from cstewart90/OpenWithSublimeText3.bat
Last active March 23, 2024 01:19
Show Gist options
  • Save byBretema/29007fd14348f1d83d17c6d560733000 to your computer and use it in GitHub Desktop.
Save byBretema/29007fd14348f1d83d17c6d560733000 to your computer and use it in GitHub Desktop.
Open folders and files with Sublime Text 3 from windows explorer context menu (tested in Windows 10)
@echo off
SET st3_path=C:\Program Files\Sublime Text 3\sublime_text.exe
SET st3_label=Open with SublimeText3 !
rem add it for all file types
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "%st3_label%" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3_path%,0" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3_path% \"%%1\"" /f
rem add it for folders
@reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "%st3_label%" /f
@reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3_path%,0" /f
@reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3_path% \"%%1\"" /f
rem add it for current folder
@reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "%st3_label%" /f
@reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3_path%,0" /f
@reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3_path% \"%%v\"" /f
pause
@byBretema
Copy link
Author

The reason for the fork was to fix a bug that caused SublimeText to open the folder twice.

@jp41011
Copy link

jp41011 commented Oct 18, 2019

@cambalamas What exactly did you change here to prevent sublime from opening two windows?
I'm looking specifically at the "add it for current folder" section. This is the only one i'm using.
Currently it looks like if your previous sublime session closed with a folder left open, let's call it Folder A, then the next time you use the right click menu inside of a Folder B to "Open with Sublime" you will get two sublime windows one with Folder A and the other with Folder B.
Is there something in this version that prevents the sublime window with Folder A to be opened when you just want a sublime window with Folder B?
Although this might actually be do to Sublime Text remembering what files/folders you had open the last time you closed Sublime Text.

Update: Actually yes. If you go into your Sublime Text settings (Preferences > Settings) and set the "hot_exit": false this does fix the "issue" i was having. Although i guess it wasn't really an issue it was just Sublime Text being helpful and remembering your previous session.
Only downside to setting the "hot_exit" to false is that you do now lose that nice feature of being able to open sublime and having it open with all the files and folders you had open in your previous session.
I wonder if there is some command line argument that we can give the sublime text call in the registry command to prevent it from opening with the previous session. That way you would still be able to leave the "hot_exit" set to true like normal but when sublime gets opened through the right click context menu it would be temporarily ignored and only open one new window, ignoring the stuff you left open in your previous session.

@byBretema
Copy link
Author

byBretema commented Oct 20, 2019

It seems like the subl3 CLI has not an option to disable that option.

The better option that I can imagine right now is something like in the .bat above, change st3_path var to point to other .bat executable or something where you modify the sublime config using sed or awk (Right now I’m on Mac so can test this) before open it and setup again after open it.

The idea:

  1. Set this settings to false:
    "remember_open_files": false,
    "hot_exit": false,
  2. Open sublime:
    "C:\Program Files\Sublime Text 3\sublime_text.exe"
  3. Reset to previous settings to the next time that you open without right click:
    "remember_open_files": true,
    "hot_exit": true,

Let me know if this helps,
Regards!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment