Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kurttomlinson/7503731 to your computer and use it in GitHub Desktop.
Save kurttomlinson/7503731 to your computer and use it in GitHub Desktop.
Get the better_errors gem's error pages to link successfully to Sublime Text 2 on Windows. This is an improved version of Vaselinessa's solution at https://gist.github.com/Vaselinessa/5584662. This version was tested on Windows 8 with Sublime Text 2 version 2.0.2, Build 2221. This solution does not show the cmd window when opening Sublime Text 2…

You must do three things to get the better_errors gem's error pages to link successfully to Sublime Text 2 on Windows:

  1. set BetterErrors.editor
  2. build a batch file to reformat the path that better_errors supplies into a Windows file path
  3. make a registry key to handle the protocol 'subl'

Set BetterErrors.editor

In your rails projects initializes directory, add a single file (call it whatever you want; I called it better_errors.rb) whose contents is:

BetterErrors.editor = :sublime if defined? BetterErrors

Make a batch file

Create a .bat file to reformat the argument supplied by the link on the better_errors page into a Windows file path, and open its file with Sublime Text 2. (When setting your variable EXECUTABLE, set it to the path of your Sublime Text 2 installation.)

	@echo off

	set EXECUTABLE="C:\Program Files\Sublime Text 2\sublime_text.exe"

	setlocal EnableDelayedExpansion

	rem fetch the command. it is the first argument
	set dest=%1

	rem convert %3A to colons
	set dest=!dest:%%3A=:!

	rem convert %2F to backslashes
	set dest=!dest:%%2F=\!

	rem convert &line to colon
	set dest=!dest:^&line=:!

	rem remove equals sign with a "while" loop
	rem equals signs are a pain to remove from strings...
	set str=!dest!
	:LoopStart
	REM Do something
	set str=!str:~1!
	set char=!str!
	set char=!char:~0,1!
	IF !char!=== GOTO DoNotConcatenate
	IF !char!==^" GOTO DoNotConcatenate
	set newstr=!newstr!!char!
	:DoNotConcatenate
	REM Break from the loop if a condition is met
	IF !char!==^" GOTO LoopEnd
	REM Iterate through the loop once more if the condition wasn't met
	GOTO LoopStart
	:LoopEnd

	rem trim first 23 characters (subl://open/?urlfile://)
	set dest=!newstr:~23!

	start "" %EXECUTABLE% "!dest!"

Save this file wherever you please. We shall reference it in the next step.

Make a registry key

Run regedit to open your Windows registry editor. Create the following key:

HKEY_CLASSES_ROOT/
  subl/
    (Default)    "URL:subl Protocol"
    URL Protocol ""
    shell/
      open/
        command/
          (Default) "<.bat filepath>" "%1"

In the key above, <.bat filepath> should be the path of the .bat file you created in step two.

@noinkling
Copy link

This seems to work nicely, thanks!

Just one thing to be aware of: copying the batch file from here will include indentation, and won't work as-is. Be sure to remove the indentation before saving the file.

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