-
Notifications
You must be signed in to change notification settings - Fork 0
Editor integration
- Invoking an editor through the shell
- Setting the default editor
- Using the
edit
command - Editing a method with the
edit-method
command - Integrating Pry into Emacs
Although Pry has reasonable support for editing and modifying code, its functionality in this regard cannot compare to a bona fide editor. As a result Pry has fairly good editor integration to give you the best of both worlds. Warning: Pry cannot translate a bash 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.
### Invoking an editor through the shellThis 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_}
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
and edit-method
.
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}" }
The edit
command is used to invoke your default editor. This command
has some advantages over using shell integration as it can immediately
"eval" or "require" your file once you have finished editing it.
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 <filename>
oredit <filename>:<line-number>
, to specify which file to open. -
Use
edit --in 1..2
to specify a range of the Pry input buffer to edit. -
Use the
-t
switch to open a temporary empty file in an editor. -
Use 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. -
Use the
-h
switch to display help. -
Use the
-l
switch to jump to the specified line number. -
Use the
-n
switch to stop the automatic reloading of.rb
files after you have edited them. -
Use the
-r
switch to force Pry to reload and eval a file, even if it does not end in.rb
. -
Use the
-c
switch to open the 'current' file/line (as shown bywhereami
command) in an editor.
For additional information on the edit --ex
functionality, see the Exception handling section.
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.
### Editing a methodYou can use edit-method Class#method
or edit-method 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.
edit-method
accepts the following options:
- Use the
-p
switch to monkey-patch the method. Instead of opening the file in which the method is defined, this will open a temporary file containing only the method itself. - 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 (usingload
) of the file's contents after editing. - Use
--no-jump
to not fast forward editor to first line of method.
Note that, as in the case of show-method
, you can simply type edit-method
(with no arguments) to edit the 'current method' (when Pry is invoked inside a method using binding.pry
at runtime).
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-method -p Pig#say_hello
Edit the say_hello
method in your text editor:
pry(main)> new_piggy = Pig.new
pry(main)> new_piggy.say_hello
=> :oink
Example:
pry(main)> edit-method -n Grit::Git#apply_patch
Example: use -M to edit methods defined on instances of a class:
pry(main)> cd Time
pry(main)> edit-method -M to_date
Example: use -m to edit methods defined on any object:
pry(main)> cd Time.new
pry(main)> edit-method -m to_date
NOTE: If you're using Ruby 1.9, you can use edit-method
on methods defined from the Pry console. As these methods have no associated file, this implies the -p
switch. This functionality doesn't yet work for Ruby 1.8.