-
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
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.
### 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 supports
options to eval the file contents immediately after the editor exits as
well as wrapping the editor in a standard interface.
The edit
command accepts the file name as parameter as well the following
options:
- Use the
-r
switch to reload file contents (eval
it) after editor exits. - Use the
-p
switch to reload file contents (usingplay
) after editor exits. - Use the
-l
switch to jump to the specified line number. - Use the
-t
switch to open a temporary file in an editor and eval it in the current context after saving and closing. - Use the
--ex
switch to open the relevant file at the line that generated the last exception. - Use the
-h
switch to display help.
Example:
pry(main) edit hello.rb -r -l 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
You 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
-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.
Example:
pry(main)> edit-method -n Grit::Git#apply_patch