How to set subl:// protocol handler with Unity?

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


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