I am looking for a way to right click a file in Windows 10, select "Open with" -> "Sublime Text", and then have a new Sublime Text window appear.
The default action is to open the file in a new tab, if Sublime is already open. This is usually not what I want, especially since I usually have Sublime open on a different desktop.
I've tried making a shortcut with the -n flag added, which correctly gives me a new window when just using the shortcut. But when I "Open with" using that shortcut it opens the file in an existing window.
I also tried "Open with" a batch file containing subl -n, ``but alas.
This works for me (and is more comfortable for me than the Open with submenu):
Open regedit.exe
navigate to HKEY_CLASSES_ROOT\*\shell
To apply this only to certain file types replace the * with the appropiate extension
Add key "Open With sublime in new window"
Inside this, add new key "command"
Inside "command" set the (Default) string to:
C:\Program Files\Sublime Text 2\sublime_text.exe -n "%1"
(basically copy the original keys, adding the -n)
Optional:
Inside the key "Open With sublime in new window", add a string named Icon:
C:\Program Files\Sublime Text 2\sublime_text.exe,0
This will add an the Sublime Text icon to the left of the command on the right-click menu.
To add the entries to the context menu for folders, add the same entries in:
HKEY_CLASSES_ROOT\Folder\shell
For the context menu when you click on a folder icon
HKEY_CLASSES_ROOT\Directory\Background\shell
For the context menu when you click on the background of a folder
For this entry you must replace "%1" with "%V" (Thanks Filip)
If you prefer a script to do this, consider using the following in a batch script:
#echo off
SET st3Path=C:\Program Files\Sublime Text 3\sublime_text.exe
rem add it for folders
#reg add "HKEY_CLASSES_ROOT\Directory\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "Open with Sublime" /f
#reg add "HKEY_CLASSES_ROOT\Directory\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3Path%,0" /f
#reg add "HKEY_CLASSES_ROOT\Directory\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3Path% \"%%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 "Open with Sublime" /f
#reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3Path%,0" /f
#reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3Path% \"%%v\"" /f
pause
Put this in a .bat file (mine is called SetSublimeMenuShortcuts.bat) and then run in Powershell as an administrator user invoking it with .\SetSublimeMenuShortcuts.bat. If you see the following, it should be set properly:
The operation completed successfully.
The operation completed successfully.
The operation completed successfully.
The operation completed successfully.
The operation completed successfully.
The operation completed successfully.
Press any key to continue . . .
Open settings, then add the following line between { }:
"open_files_in_new_window": false
Related
I want to add the ability to open a folder with Sublime Text to the context menu, as it is by default in VS Code, I tried the following script:
#echo off
SET st3Path=C:\Program Files\Sublime Text 3\sublime_text.exe
rem add it for folders
#reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "Open with Sublime Text 3" /f
#reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3Path%,0" /f
#reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3Path% \"%%1\"" /f
pause
This works, but every time I open a folder, all previous folders open in separate windows too. Can I fix it somehow?
You should use subl.exe and not sublime_text.exe for carrying out this action; subl is meant for interacting with the running instance of Sublime (and will start it if it's not already running), while sublime_text is the application itself.
Regardless of that however, your issue is also (somewhat) related to this setting, which as seen here defaults to being turned on:
// Exiting the application with hot_exit enabled will cause it to close
// immediately without prompting. Unsaved modifications and open files will
// be preserved and restored when next starting.
//
// Closing a window with an associated project will also close the window
// without prompting, preserving unsaved changes in the workspace file
// alongside the project.
"hot_exit": true,
When this is enabled you can quit Sublime at any point and have it save it's session (including unsaved file changes, selection states, etc) and then restore that to put you back to where you were.
So, if you previously quit Sublime while you had windows open, then when you carry out your action above Sublime will restore it's session (and all other windows) and then create the new one.
Turning the setting off stops that from happening so that every startup of Sublime is a "clean" state with no restored windows or state. However this also implies that when you quit Sublime you need to ensure that you save changes or your work will be lost.
Given your provided information and based upon the information provided in the command help, reg add /?, I'd assume that your batch file should look something like this:
#Set "st3Path=%%Program Files%%\Sublime Text 3\sublime_text.exe"
#Set "regPath=HKCU\Software\Classes\Folder\shell\Open with Sublime Text 3"
#%__AppDir__%reg.exe Add "%regPath%" /VE /D "Open with Sublime Text 3" /F >NUL
#%__AppDir__%reg.exe Add "%regPath%" /V Icon /T REG_EXPAND_SZ /D "\"%st3Path%\",0" /F >NUL
#%__AppDir__%reg.exe Add "%regPath%\command" /VE /D "\"%st3Path%\" \"%%~1\"" /F >NUL
As for your Software opening previously opened 'folders', I'm sure that, you can configure the software not to reopen previous files on startup, directly in the Software or by editing its config files. That is therefore outside of the scope of your code issue, and to be dealt with separately.
You'll notice that I've used the 'Current User' registry, not the 'Local Machine', as it doesn't require to be 'Run as administrator' and you shouldn't be globally making personalization changes; not everyone wants them.
If you wish to do it for all users, however, just replace HKCU on line 2 with HKLM.
Please also note, that I have no knowledge of your software itself, so taking account of the information provided in the OdatNurd's answer, please consider whether to replace sublime_text.exe on line 1 with subl.exe
For sublime 4 (not sure about sublime 3) on windows, you just need to run the setup file again.
It will ask you if you want to add it to context menu.
I do not have administrator privileges at office and I would like to add Sublime Text to contextual menu to edit easily my files such as Notepad++. Is it possible? I've only found how to add it with Administrators privileges
Often we do not have administrator privileges at office on our PC, so some weeks ago I found a useful guide to add Sublime to the contextual menu.
First you need create a new text document with any text editor, copy and paste the below script, save it on your desktop with .bat extension
#reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "Open with Sublime Text 3" /f
#reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3Path%,0" /f
#reg add "HKEY_CURRENT_USER\Software\Classes\*\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3Path% \"%%1\"" /f
Keep in mind that you must replace %st3Path% with your current SublimeText directory, e.g: C:/Tools/sublime/sublimetext.exe
Double-click on that file to apply the settings. Now you can see Sublime in the context menu.
On the other hand we may need to remove Sublime from contextual menu, for to do that you can use the following script, and repeat the same steps above:
#reg delete "HKEY_CURRENT_USER\Software\Classes\*\shell\Open with Sublime Text 3\command" /f >nul 2>&1
#reg delete "HKEY_CURRENT_USER\Software\Classes\*\shell\Open with Sublime Text 3" /f >nul 2>&1
Or if you prefer, here's the script ready to use it: SublimeText-ContextualMenu.bat
Is there a way i can associate a file type like (.php, .css, .js) to a custom icon, even when the file has been associate with a program.
for example on my computer .php is associated to sublime text, and when i encounter .php on my file system it show ST's icon, is there a way to make it change to a custom icon like the good ol' elephant, but still be associated with ST i.e when i click the file it still open's in ST(== sublime text).
I know how to do it for filetypes assigned to Notepad++.
In Windows 10, Notepad++ keeps hijacking the icons of filetypes that you open (by default) in Notepad++. It was very difficult to break that behaviour, but I finally got it working!
Preparations
1. Registry permissions
Open the Windos registry, and give yourself enough "permissions" for the folders HKEY_CLASSES_ROOT and HKEY_CURRENT_USER. I'm not sure myself what are the minimum settings for the whole procedure to succeed. You've got to play a bit with the settings. Anytime you get an error message in the coming procedure, go back to this step and increase your registry permissions.
2. Make some custom icons
Make some custom icons, for example in the folder C:\ICONS\. I use the following website https://iconverticons.com/online/ to convert 256x256 png-files into ico-files.
3. Install Notepad++
Just get Notepad++. Nothing special here.
4. Open a terminal with admin rights
Type cmd in the windows search bar. Right click on the CMD-icon that appears and choose "Run as administrator" in the popup window.
Registry hacking
Disclaimer: changing your registry can potentially cause damage to your Windows installation. I have written down the procedure below with best intentions, but I deny any responsibility in case something goes wrong.
Note: I DON'T recommend to use this procedure for .bat files. This is a very important file-type for Windows. So I don't know what would happen if you delete its standard registry keys.
The procedure below describes how to assign a custom icon - which you created in C:\ICONS\abcfile.ico - to all *.abc files.
1. Registry hacking, part one
The first registry key you should add is HKEY_CLASSES_ROOT\.abc. In case this key already exists, you might want to delete it. Use the following command to delete the key and all its subkeys:
> REG DELETE HKEY_CLASSES_ROOT\.abc /f
The following figure shows how we will (re)build this registry key and all its subkeys:
You can do it manually or run the following commands:
> REG ADD HKEY_CLASSES_ROOT\.abc /ve /t REG_SZ /d "abc_auto_file"
> REG ADD HKEY_CLASSES_ROOT\.abc /v BrowserFlags /t REG_DWORD /d 0x00000000
> REG ADD HKEY_CLASSES_ROOT\.abc /v EditFlags /t REG_DWORD /d 0x00000000
> REG ADD HKEY_CLASSES_ROOT\.abc /v PerceivedType /t REG_SZ /d "text"
> REG ADD HKEY_CLASSES_ROOT\.abc\DefaultIcon /ve /t REG_SZ /d "C:\ICONS\abcfile.ico,0"
2. Registry hacking, part two
The second registry key you should add is HKEY_CLASSES_ROOT\abc_auto_file. In case this key already exists, you might want to delete it. Use the following command to delete the key and all its subkeys:
> REG DELETE HKEY_CLASSES_ROOT\abc_auto_file /f
The following figure shows how we will (re)build this registry key and all its subkeys:
You can do it manually or run the following commands:
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\DefaultIcon /ve /t REG_SZ /d "C:\ICONS\abcfile.ico"
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\edit /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\edit\command /ve /t
> REG_EXPAND_SZ /d "\"C:\Program Files (x86)\Notepad++\notepad++.exe\" \"^%1\""
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\open /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\open\command /ve /t
> REG_EXPAND_SZ /d "\"C:\Program Files (x86)\Notepad++\notepad++.exe\" \"^%1\""
3. Registry hacking, part three
The second registry key you should add is HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc. In case this key already exists, you might want to delete it. Use the following command to delete the key and all its subkeys:
> REG DELETE HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc /f
The following figure shows how we will (re)build this registry key and all its subkeys:
You can do it manually or run the following commands:
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc /ve /t REG_SZ
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithList /ve /t REG_SZ
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithProgids /ve /t REG_SZ
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithProgids /v "C:\Program Files (x86)\Notepad++\notepad++.exe" /t REG_NONE /d 0
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithProgids /v abc_auto_file /t REG_NONE /d 0
Refresh icon cache
The following command should refresh the Windows icon cache:
> %windir%\system32\ie4uinit.exe -show
Assign Notepad++ to the filetype
I've noticed that - sometimes - the changes only take effect after assigning Notepad++ as the default program to open the filetype.
This is how to do that:
Create a file with the given filetype, eg: myFile.abc
Right click on the file, and select "open with" >> "another app"
You should get a popup window like:
Select Notepad++ as the default program
Finish
Normally the icon has changed now! And all files of that particular filetype will have the custom icon. They will open by default in Notepad++.
Perhaps you should again refresh the icon cache:
> %windir%\system32\ie4uinit.exe -show
or restart your system to let it take effect (although restarting wasn't needed for me).
Is there a way to set Sublime Text as the default text editor for file formats on Windows 7?
Also, if anyone knew a Sublime Text Tutorial or Wiki that would be really helpful.
Actually it is not my answer, I have just googled it:
Open regedit (Win+R, type "regedit", select OK).
Navigate to HKEY_CLASSES_ROOT\Applications\sublime_text.exe\shell\open\command
Verify that the path is accurate, correct it if it is not. Exit regedit.
Open task manager via Ctrl+Alt+Del (or Ctrl+Shift+Esc for later versions of Windows), kill explorer.exe, go to run (Win+R) and type "explorer.exe" (or skip this step and simply reboot).
Now attempt the same thing, right click a text file, open with, navigate to sublime, and it should now appear in the list of available applications.
For me that value pointed to the Desktop where portable version previously was placed. Thus it just didn't work.
P.S. And for me the reboot or logout were not necessary (WinXP).
Edit on Nov 21, 2014
Tim Lewis pointed out in the comment that there is a more generic version at http://www.binaryfortress.com/NotepadReplacer/, which works better.
Original Answer
Try this: https://github.com/grumpydev/Sublime-Notepad-Replacement
Here are some ways to associate Sublime Text Portable. The following text needs to be saved as a file with a .reg extension and then on that file Right Click > Merge.
This will add a Sublime right click menu entry to all files:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell]
#="Sublime"
[HKEY_CLASSES_ROOT\*\shell\Sublime]
#="&Sublime"
[HKEY_CLASSES_ROOT\*\shell\Sublime\command]
#="\"D:\\PortableApps\\SublimeText\\sublime_text.exe\" \"%1\""
This will have Sublime Text replace all calls to notepad.exe:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\Notepad.exe]
"Debugger"="\"D:\\PortableApps\\SublimeText\\sublime_text.exe\" -z"
This will create a SublimeFile class which you can then associate with any extension.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\SublimeFile\shell]
#="edit"
[HKEY_CLASSES_ROOT\SublimeFile\DefaultIcon]
#="\"D:\\PortableApps\\SublimeText\\sublime_text.exe\",0"
[HKEY_CLASSES_ROOT\SublimeFile\shell\edit\command]
#="\"D:\\PortableApps\\SublimeText\\sublime_text.exe\" \"%1\""
This will then associate the .ext extension with SublimeFile:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.ext]
#="SublimeFile"
1: You can also set your associations in Control Panel:
Control Panel > Default Programs > Associate a file or protocol with a specific program:
2: Or, call it from code via IApplicationAssociationRegistrationUI::LaunchAdvancedAssociationUI ;)
HRESULT LaunchAdvancedAssociationUI(
[in] LPCWSTR pszAppRegName
);
You can just run CCleaner's registry cleaner tool and then choose and add the default program as usual. It worked for me when upgrading from ST2 to ST3.
Open context menu on file with desired extension in Explorer, than select Open with->You editor ( may be you need to browse for its binary before it appears in the list of available programs ) and set checkbox "Always use the selected program to open this kind of file".
I tried all options to make sublime text 3 as the default program for my .php, .css and/or .js files
I don't exactly know why it all failed, I think it is related to windows 10 current version bug
But the good news is there is some working way for me, also for those who love CMD
open CMD as administrator
run this line: FTYPE sublime="C:\Program Files\Sublime Text 3\sublime_text.exe" "%1"
for .php files use this command: ASSOC .php=sublime for js use this: ASSOC .js=sublime and so on...
Try this,
#echo off
SET st2Path=C:\Program Files\Sublime Text 2\sublime_text.exe
rem add it for all file types
#reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 2" /t REG_SZ /v "" /d "Open with Sublime Text 2" /f
#reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 2" /t REG_EXPAND_SZ /v "Icon" /d "%st2Path%,0" /f
#reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 2\command" /t REG_SZ /v "" /d "%st2Path% \"%%1\"" /f
rem add it for folders
#reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 2" /t REG_SZ /v "" /d "Open with Sublime Text 2" /f
#reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 2" /t REG_EXPAND_SZ /v "Icon" /d "%st2Path%,0" /f
#reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 2\command" /t REG_SZ /v "" /d "%st2Path% \"%%1\"" /f
pause
I tested this for SublimeText 3(Portable) also and working fine. Create a .bat file with the above code and run it as administrator.
Reference : https://gist.github.com/mrchief/5628677
sublime can be set as the default text editor by following these steps:
right-click on the .txt file and select properties.
then in Open with: option click on Change.
if the sublime is listed in the list of apps then select it, if not scroll down and click on the look for another app on this PC and select the sublime app file
I was facing the same problem.
the only solution was correct it manually
--> open regedit and navigate to Computer\HKEY_CLASSES_ROOT\Applications\, find the sublime_text.exe entry, and delete it. You should now be able to set Sublime Text 3 as the default editor for anything you want.
i found this solution in this post:https://forum.sublimetext.com/t/cant-make-default-editor-in-windows/10747/14
If you are using the portable version, you can't set Sublime as the default program for any files because Windows won't acknowledge it as a program that can open things.
i have a dostuff.cmd file that takes one argument and looks something like this:
filepath_to_command_to_run %1
If I type dostuff.cmd FILE into the command prompt it runs fine.
Is it even remotely possible to get it so that I can associate that file type with my dostuff.cmd so that if I double click a file the .cmd will get it as an argument? Right now I've associated the file type with it, but when I double click it runs the stuff in the .cmd without getting the file as an argument.
Right click on the file -> Open With -> Choose Default Program -> Browse to and select your cmd file. ( Choose the checkbox saying always use this for this file type if you want)
Now double clicking the file will open it with the cmd file.
Alternatively, you can set it directly to filepath_to_command_to_run
Once I have achieved the correct way of FILE ASSOCIATION using these cmd commands.
this is just an example:
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v # /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f
assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"
(it's better to put them in .bat file)