Skip to content
This repository

Editor integration

Quick Menu:

Back to Main Menu

Overview

Pry has a shell-like prompt that can do some things an editor can't (like tab-complete actual methods of an object), but sometimes you'll need to use a traditional editor.

Using the edit command

The edit command is used to invoke your default editor. This command will load your file once you have finished editing it (unless you pass the -n or --no-reload flag to edit).

Pry also has some short-cuts for opening files you might need.

The edit command, as well as changing Pry's recent input, can be used to open any file. You can use edit with:

  • The -c switch to open the 'current' file/line (as shown by whereami command, such as when Pry is invoked inside a method using binding.pry at runtime).
  • Class#a_method or Class.a_class_method (or my_method if the method is in scope) to open a method for editing directly in the default editor. Pry will attempt to jump to the line in the file where the method is defined. ** This accepts the -p switch to open a temporary file containing only the method, then patch that edit in (without modifying the actual file).
  • <filename> or <filename>:<line-number>, to specify which file to open.
  • The --in 1..2 flag to specify a range of the Pry input buffer to edit.
  • The -t switch to open a temporary empty file in an editor.
  • The --ex switch to open the relevant file at the line that generated the last exception.

    HINT: Use --ex N if you want the Nth line of the backtrace, just like cat --ex.

  • The -l switch to jump to the specified line number.
  • The -n switch to stop the automatic reloading of .rb files after you have edited them.
  • The -r switch to force Pry to reload and eval a file, even if it does not end in .rb.

For additional information on the edit --ex functionality, see the Exception handling section.

Being run without any arguments, the edit command modifies the last input expression.

Example: Jump to line 10 in the file, Pry will reload the file after you have finished

pry(main) edit hello.rb:10

Example: edit --ex causes the file blah.rb to be opened at line 7

pry(main)> hello
NameError: undefined local variable or method `error' for main:Object
from /Users/john/blah.rb:7:in `hello'
pry(main)> edit --ex

Example: edit -n stops Pry from reloading the file.

pry(main)> edit -n ../other-project/other_project.rb

HINT: if you're peeking into files belonging to other projects, you should consider using the cat command instead.

  • Use the -M switch to edit methods defined on instances of a class.
  • Use the -m switch to edit methods defined on the object itself.
  • Use the -s option to select the super method. You can repeat -ss to get to the super method's super method.
  • Use the -n switch to prevent reloading (using load) of the file's contents after editing.
  • Use --no-jump to not fast forward editor to first line of method.

Example: use -p to fix a pig's vocal organ:

# pig.rb

class Pig
  def say_hello
    :woof
  end

  def eat
    "Om nom nom"
  end
end

piggy = Pig.new

binding.pry
> ruby -rpry pig.rb
pry(main)> piggy.say_hello
=> :woof
pry(main)> edit -p Pig#say_hello

Edit the say_hello method in your text editor:

Editing #say_hello

pry(main)> new_piggy = Pig.new
pry(main)> new_piggy.say_hello
=> :oink

(Note that this didn't change pig.rb, only an in-memory version of it.)

Example:

pry(main)> edit -n Grit::Git#apply_patch

Back to top

Setting the default editor

As an alternative to invoking an editor through the shell you can also set a default editor for Pry.

The Pry.config.editor variable defaults to the environment variable $EDITOR (or nano if $EDITOR is not defined). If you set it to a String then that string is used as the shell command to invoke the editor. If you set it to a callable (e.g a Proc) then file and line are passed in as parameters and the return value of that callable invocation is used as the exact shell command to invoke the editor.

The value of Pry.config.editor is then used by commands such as edit.

Note that it maybe be convenient to set your desired editor in the .pryrc file or else in the $EDITOR environment variable.

Example: Setting a String

Pry.config.editor = "emacsclient"

Example: Setting a proc

Pry.config.editor = proc { |file, line| "emacsclient +#{line} #{file}" }

Note: Pry cannot translate a shell alias into a command because of the way Ruby works, so if you use an alias such as 'vi=vim' set your EDITOR env to vim and not vi.

Back to top

Invoking an editor through the shell

This is the simplest way to start an editor from Pry. As stated in the shell integration section any input prefixed by a . is sent to the shell; so one way to open an editor in Pry is just to enter the name of the editor prefixed by a .

Example:

pry(main)> .vim test_file.rb

Example: Interpolate file name

pry(main)> _file_
=> "/Users/john/.rvm/gems/ruby-1.9.2-p180/gems/grit-2.4.1/lib/grit/blob.rb"
pry(main)> .emacsclient #{_file_}

Back to top

Something went wrong with that request. Please try again.