[NCLUG] stuff related to Vim presentation/talk

Chad Perrin perrin at apotheon.com
Tue Sep 10 21:34:25 MDT 2013


When I mentioned that I had set up Standard ML REPL integration with Vim
at the NCLUG meeting tonight, someone (don't recall exactly who) asked
if I could send "that" to the list.  I think he was asking about the SML
setup, which is just one line in my .vimrc file:

    autocmd BufEnter *.sml cabbrev ml !sml %:p

This allows me to use an "ml" command to fire up the SML REPL from
within Vim when editing an SML file, and the contents of the file will
be sent to (and executed by) the REPL.  When I exit the REPL, then, I'm
sent back to Vim.

The autocmd part automatically runs a command when the identified
conditions apply (in this case, you open a file conforming to the
filename pattern *.sml).  The command is cabbrev, which creates a
command abbreviation for you (ml, as I've defined it this time), which
expands to a more complex command sequence (shelling out to the sml
command used to start the REPL, passing the contents of the buffer).

I also have a Ruby gem called interactive_editor that provides similar
integration with Vim, but sorta in reverse.  It allows me to run irb
(the Ruby REPL), then open Vim to edit code.  When I save and exit, the
code comes back to irb with me to be executed.  Thus, where the SML
config above allows me to conceptually embed the SML REPL in Vim, the
interactive_editor allows me to conceptually embed Vim within the Ruby
REPL.  It also works for other editors, by the way.

Experienced Rubyists will know that the way to install a typical Ruby
gem is pretty straightforward:

    gem install interactive_editor

You can either explicitly load interactive_editor within your irb
session, like so:

    require 'interactive_editor'

. . . or add that line of code to your .irbrc file so it will do that
for you automatically any time you open irb.  Either way, with
interactive_editor loaded in your irb session, just enter the `vim`
command to start Vim and start playing with code, giving it a filename
as an argument if you want to operate on a file that will be saved
outside of the irb session, thus providing a sort of inside-out IDE for
Ruby:

    vim 'filename.rb'

The interactive_editor gem also supports other editors, if they are
installed.  It's a pretty short bit of code, so you should be able to
just open up the interactive_editor.rb file on your system or look at it
on GitHub to see, in pretty clear code, what editors are configured by
default -- or, on your system, to add editors if you want to try that.
I'm pretty sure it offers some way to add editors without editing the
source file or monkeypatching the relevant code directly, but I do not
remember how one would do that (or even for sure whether it would be
possible, as written) off the top of my head.  I've only ever used nvi
and Vim with it, after all.

I do remember, however, that if you set your shell's EDITOR environment
variable to something sane for your purposes, then use the `ed` method
provided by interactive_editor by default (instead of the `vim` method
as I described above for Vim), interactive_editor will attempt to use
the editor identified with the EDITOR environment variable.  In the
config file for irb (.irbrc), you should be able to set that environment
variable *only* within irb sessions with this:

    ENV['EDITOR'] = 'xedit'

I doubt you'll use xedit, but what the heck -- it's an example of an
editor.

The GitHub repository for interactive_editor is at . . .

    https://github.com/jberkel/interactive_editor

I hope some of the above helps someone.

-- 
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]


More information about the NCLUG mailing list