6

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?

  • Have a look a this answer and just replace txmt with subl where needed. – Bogdan Oct 29 '14 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 '14 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 '14 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 '14 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 '14 at 15:54
5

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

| improve this answer | |
  • 1
    i used this technique to register a protocol for kate text editor like kate:// and it works perfect! – Silver Moon Jun 1 '18 at 8:44
  • This works well also in Firefox! – xonya Oct 11 '19 at 20:58
1

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
| improve this answer | |

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.