-
Notifications
You must be signed in to change notification settings - Fork 605
Editor integration
- Using the
editcommand - Setting the default editor
- Invoking an editor through the shell
- Integrating Pry into Emacs
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.
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
-cswitch to open the 'current' file/line (as shown bywhereamicommand, such as when Pry is invoked inside a method usingbinding.pryat runtime). -
Class#a_methodorClass.a_class_method(ormy_methodif 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-pswitch 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..2flag to specify a range of the Pry input buffer to edit. -
The
-tswitch to open a temporary empty file in an editor. -
The
--exswitch to open the relevant file at the line that generated the last exception.HINT: Use
--ex Nif you want the Nth line of the backtrace, just like cat --ex. -
The
-lswitch to jump to the specified line number. -
The
-nswitch to stop the automatic reloading of.rbfiles after you have edited them. -
The
-rswitch 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
-Mswitch to edit methods defined on instances of a class. - Use the
-mswitch to edit methods defined on the object itself. - Use the
-soption to select the super method. You can repeat-ssto get to the super method's super method. - Use the
-nswitch to prevent reloading (usingload) of the file's contents after editing. - Use
--no-jumpto 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:
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
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 $VISUAL or $EDITOR (in that order). If neither of them is defined, it will try to use the default editor of your platform (usually nano). 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 $VISUAL or $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.
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_}