Skip to content

Link to your editor

yoche2001 edited this page Jan 19, 2022 · 38 revisions

Better Errors includes a link to the source code when you're looking at any exception.

link directly to the line and file in your editor

Configuring

By default, your $EDITOR environment variable is used to determine which editing application you prefer. If you haven't set $EDITOR, that would be a good first step for making development easier.

If you prefer that Better Errors use a different editor than the one in your $EDITOR, set the $BETTER_ERRORS_EDITOR environment variable. It will take precedence. (But this is supported only by v2.9 and higher.)

Some editors require you to take steps to install their command-line tool.

Then you'll need to modify your ~/.bashrc or ~/.zshrc depending on which shell you use, to set $EDITOR.

If you are using Pow or puma-dev for managing multiple web applications see Running in Puma dev or Pow.

If you are using Vagrant, Docker, or another virtualization mechanism, you may need to configure it to have the correct location of the source files on your machine. See Running on virtual machines.

If your editor is not supported, we strongly recommend against modifying your software project to point to a specific editor. This is because every developer might have different preferences.

Supported editors

Better Errors supports the following editors.

See BetterErrors::Editor::KNOWN_EDITORS for more information. If your $EDITOR matches the sniff pattern, the listed editor will be used.

If your editor is not listed, you can instead set $BETTER_ERRORS_EDITOR_URL to a string like the urls listed there. By doing this, you can customize the URL to anything you want, including cloud editors. (But this is supported only by v2.9 and higher.)

Atom

Atom v1.23 added support for open/file URLs.

Install the Shell Commands from within Atom and add the following to your shell config:

export EDITOR="atom"

Emacs

Add the following to your shell config:

export EDITOR="emacs"

The URL is emacs://open?url=file://%{file}&line=%{line}.

Using Firefox

You'll need to register the emacs:// protocol in Firefox. Also, Firefox will pass a lot of junk to the command, here's a suggestion for your bin/ folder: https://gist.github.com/nofxx/6987409

IntelliJ IDEA

Add the following to your shell config:

export EDITOR="idea"

The URL is idea://open?file=%{file}&line=%{line}.

MacVIM

Add the following to your shell config:

export EDITOR="vim"

The URL is mvim://open?url=file://%{file}&line=%{line}.

In iTerm

Instructions are in phallstrom/urlscheme_vim_in_iterm.

RubyMine

Add the following to your shell config:

export EDITOR="rubymine"

The URL is rubymine://open?file=%{file}&line=%{line}. If this doesn't work, please try x-mine://open?file=%{file}&line=%{line}.

In newer version of RubyMine you have to enable the Command-Line Launcher first. This can be done via the Toolbox App (Jetbrain’s tool to manage their products updates and configurations).

Sublime Text

Add the following to your shell config:

export EDITOR="subl -w"

You'll need to install subl handler or something similar.

If you installed a sublime URL handler before August 2017, please see this security advisory and remove your old URL handler.

(The -w causes the command line tool to wait until you close the file before it continues. This is necessary for tools like git, where it must wait for you to edit the file before continuing.)

The URL is subl://open?url=file://%{file}&line=%{line}.

TextMate

TextMate-compatible URLs are enabled by default, or if $EDITOR contains "mate". The URL is txmt://open?url=file://%{file}&line=%{line}.

Add the following to your shell config:

export EDITOR="mate -w"

(The -w causes the command line tool to wait until you close the file before it continues. This is necessary for tools like git, where it must wait for you to edit the file before continuing.)

Visual Studio Code

Install the command-line tools

Add the following to your shell config (on MacOS put this in ~/.bash_profile :

export EDITOR="code --wait"

(The --wait causes the command line tool to wait until you close the file before it continues. This is necessary for tools like git, where it must wait for you to edit the file before continuing. Also very handy for credentials.yml.enc in Rails >5.2).

For VS Code remote targets under WSL, the following initializer works (see below):

if defined?(BetterErrors) && (distro_name = ENV['WSL_DISTRO_NAME'])
  BetterErrors.editor = "vscode://vscode-remote/wsl+#{distro_name}%{file_unencoded}:%{line}"
end

Editors not supported

Is your preferred editor missing? Please open an Issue or Pull Request.

Panic Nova

Nova does not currently support opening files through a nova:// scheme URL.

Forcing a specific editor

You can configure your Ruby project to force Better Errors to link to a specific editor.

But this is not recommended, since each developer on your project may prefer a different editor.

If you must configure Better Errors this way, add config/initializers/better_errors.rb to your global .gitignore or the project .gitignore file.

Place the following in config/initializers/better_errors.rb:

if defined?(BetterErrors)
  BetterErrors.editor = :txmt
end

If you're using an editor that is not supported directly by Better Errors, you can provide a formatting string, for example:

if defined?(BetterErrors)
  BetterErrors.editor = "vscode://file/%{file}:%{line}"
end

Or if you need to change the way the string is assembled, you can provide a proc, for example:

if defined?(BetterErrors)
  BetterErrors.editor = proc { |file, line|
    "vscode://file/%{file}:%{line}" % { file: URI.encode_www_form_component(file), line: line }
  }
end