8

My web app generates clickable URLs like the following when an error occurs:

subl://open?url=file://%2Fusr%2Flocal%2Fwebenginex%2Fclass%2Fmodels%2Fwxclient.php&line=123

When I click it I get this prompt:

Clicking "Launch Application" does nothing. I want it to open Sublime Text as intended. How can I configure that?

9
  • Have a look a this answer and just replace txmt with subl where needed.
    – Bogdan
    Oct 29, 2014 at 20:30
  • @Bogdan Thanks, but when I run sudo update-desktop-database I get "Warning in file "/usr/share/applications/sublime-text-2.desktop": usage of MIME type "x-directory/normal" is discouraged ("x-directory" is an old media type that should be replaced with a modern equivalent)" and clicking a subl:// link still gives this popup.
    – mpen
    Oct 29, 2014 at 21:55
  • The warning shouldn't be an issue, it's only compaining about the use of a deprecated mime type and it's not in the sublime-handler.desktop file you should have created. The popup is to be expected because Chrome doesn't know how to handle special protocols like subl, so it asks if you want to use xdg-open (which is ok in this case). Just click on Launch Application and it should open the file in Sublime Text. Of course you can check the Rememeber my choice for all links of this type so next time you won't be prompted.
    – Bogdan
    Oct 29, 2014 at 22:14
  • @Bogdan Oh..I should have paid closer attention; I thought that was the file I created. I did try clicking Launch Application though, didn't do anything. Maybe I have to reboot...
    – mpen
    Oct 29, 2014 at 23:16
  • 1
    Maybe, I can't say for sure. I'm on 12.04 and for me it worked out of the box. At least the problem's solved :)
    – Bogdan
    Oct 30, 2014 at 15:54

3 Answers 3

10

To register custom protocol handler in chrome

1. Create the desktop file

Create the file /usr/share/applications/sublime-handler.desktop

[Desktop Entry]
Name=Sublime Text 2 URL Handler
GenericName=Text Editor
Comment=Handle URL Scheme subl://
Exec=/usr/share/handlers/sublime-handler %u
Terminal=false
Type=Application
MimeType=x-scheme-handler/subl;
Icon=sublime-text-2
Categories=TextEditor;Development;Utility;
Name[en_US]=Sublime Text 2 URL Handler

2. Update the MIME-types database

$ sudo update-desktop-database

3. Create the handler file

Create the file /usr/share/handlers/sublime-handler

#!/usr/bin/env bash

request="${1#*://}"             # Remove schema from url (subl://)
request="${request#*?url=}"     # Remove open?url=
request="${request//%2F//}"     # Replace %2F with /
request="${request/&line=/:}"   # Replace &line= with :
request="${request/&column=/:}" # Replace &column= with :

subl "$request"                 # Launch sublime

Make it executable:

$ sudo chmod +x /usr/share/handlers/sublime-handler

4. Register mime-type handler

$ xdg-mime default /usr/share/applications/sublime-handler.desktop x-scheme-handler/subl

5. Profit

Now you can open links with custom protocols from chrome via custom applications, example:

subl:///home/path/to/file.php:123
subl://open?url=/home/path/to/file.php:123
subl://open?url=/home/path/to/file.php&line=123
subl://open?url=/home/path/to/file.php&column=123
subl://open?url=%2Fhome%2Fpath%2Fto%2Ffile.php&line=123
subl://open?url=%2Fhome%2Fpath%2Fto%2Ffile.php&column=123

6. Modify

It can be ported for using with the different IDE, for example phpstorm

2
  • 1
    i used this technique to register a protocol for kate text editor like kate:// and it works perfect! Jun 1, 2018 at 8:44
  • This works well also in Firefox!
    – xonya
    Oct 11, 2019 at 20:58
3

I am pretty much summarizing this and making it more copy-pastable...

Create the Sublime URL Parser file '/usr/local/bin/subl-url-parser':

sudo tee /usr/local/bin/subl-url-parser > /dev/null <<"EOF"
#!/usr/bin/env bash

request=${1:23}               # Delete the first 23 characters "subl://open?url=file://"
request=${request//%2F//}     # Replace %2F with /
request=${request/&line=/:}   # Replace &line= with :
request=${request/&column=/:} # Replace &column= with :
sublime $request              # Launch Sublime
EOF

Make it executable, and symlink it to "subl-url-handler":

sudo chmod +x /usr/local/bin/subl-url-parser
sudo ln -s subl-url-parser /usr/local/bin/subl-url-handler

Create (or update) the desktop file ' /usr/share/applications/sublime-handler.desktop' to use your new URL parser via 'subl-url-handler':

sudo tee /usr/share/applications/sublime-handler.desktop > /dev/null <<EOF
[Desktop Entry]
Name=Sublime Text URL Handler
GenericName=Text Editor
Comment=Handle URL Scheme subl://
Exec=subl-url-handler %u
Terminal=false
Type=Application
MimeType=x-scheme-handler/subl;
Icon=sublime-text
Categories=TextEditor;Development;Utility;
Name[en_US]=Sublime Text URL Handler
EOF

Update the databse:

sudo update-desktop-database

NB: This assumes that you have a sublime in your $PATH and not just subl, as I do. If not, you can make one like this:

sudo ln -s `which subl` $(dirname `which subl`)/sublime
1
  • very cool. can be one script actually to run blindly. Aug 12, 2021 at 6:25
0

I had the same problem with Sublime Text 3 (AKA ST3) and Andrey's answer above worked for me. I used a different dir for the handler, switched some "2"s for "3"s in labels, and I changed request="${request#*?url=}" to request="${request#*?url=file://}" but otherwise all the same.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .