Skip to content

Instantly share code, notes, and snippets.

@mctaylorpants
Last active July 21, 2025 00:08
Show Gist options
  • Save mctaylorpants/04a9353583681f48d90d4ac9f58d3485 to your computer and use it in GitHub Desktop.
Save mctaylorpants/04a9353583681f48d90d4ac9f58d3485 to your computer and use it in GitHub Desktop.

Revisions

  1. mctaylorpants revised this gist Nov 6, 2022. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions neovim_eval_ruby.rb
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,16 @@
    # with multiple lines selected, type :EvalRuby.
    #
    # Installation:
    # 1. Put this file in your plugins directory
    # 1. Install neovim-ruby: https://github.com/neovim/neovim-ruby
    #
    # 2. Put this file in your plugins directory
    # (Default: ~/.config/nvim/rplugin/ruby)
    #
    # 2. Run :UpdateRemotePlugins to register the
    # 3. Run :UpdateRemotePlugins to register the
    # new plugin.
    #
    # 3. Restart Neovim.
    # 4. Restart Neovim.
    #
    Neovim.plugin do |plug|
    plug.command(:EvalRuby, range: true) do |nvim, range_start, range_end|
    ruby_code = nvim
  2. mctaylorpants created this gist Nov 6, 2022.
    30 changes: 30 additions & 0 deletions neovim_eval_ruby.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # EvalRuby: Run Ruby without leaving Neovim
    #
    # Usage:
    # - While on a line of Ruby, or in Visual mode
    # with multiple lines selected, type :EvalRuby.
    #
    # Installation:
    # 1. Put this file in your plugins directory
    # (Default: ~/.config/nvim/rplugin/ruby)
    #
    # 2. Run :UpdateRemotePlugins to register the
    # new plugin.
    #
    # 3. Restart Neovim.
    Neovim.plugin do |plug|
    plug.command(:EvalRuby, range: true) do |nvim, range_start, range_end|
    ruby_code = nvim
    .get_current_buf
    .lines[(range_start - 1)..(range_end - 1)]
    .join("\n")

    result = begin
    eval ruby_code
    rescue => e
    "! #{e.message} (#{e.class})"
    end

    nvim.get_current_buf.append(range_end, "# => #{result.inspect}")
    end
    end