Info

Hi, I'm David Askew

I build things

Homepage

Contact Me

About

Open Source

Pry And Vim, A Match Made In Heaven

I love Pry. I’m learning to love Vim.

Every once in a while I will be using Pry to test some code and I make a spelling mistake. By “every once in a while”, I mean ever freakin day.

1
pry > puts "Helo Wurld"

At this point, assuming you have executed the line by pressing return, you have a few options:

  • Hit the up arrow to recall your previous bad command. Then use the arrow keys and delete/update the line as appropriate.
  • Execute the ‘edit’ command
  • Retype the whole line

Note: Don’t retype the whole command, use edit!

1
pry > edit

By default, Pry will launch Nano with your previous command prepopulated in the editor. You can then edit and save the command. Once you exit, the command will run in Pry. This becomes increadibily helpful for lengthy multiline statements and blocks of code.

Wouldn’t it be nice if you could use the edit command with your favorite editor? My current preference is Vim. There are 2 ways to make this a reality:

  • update the $EDITOR environment variable
  • update your pry config file (~/.pryrc)

Updating the $EDITOR environment variable will impact more than just Pry, so use it with caution. In my case, I want to use Vim wherever possible, so I have added the following line to my ~/.bashrc file.

1
export EDITOR="vim"

If you only want to use Vim with Pry then you probably just want to update the Pry config file. (Usually: ~/.pryrc)

1
Pry.config.editor = "vim"

More details about updating the Pry configuration can be found here.

I hope this helps.