Skip to content

Instantly share code, notes, and snippets.

@thinca
Created July 31, 2018 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinca/79fecf9ceeab83d648f861a02fe1037e to your computer and use it in GitHub Desktop.
Save thinca/79fecf9ceeab83d648f861a02fe1037e to your computer and use it in GitHub Desktop.
Meguro.rb #17 Ruby で Vim を拡張する

Ruby で Vim を拡張する

2018-07-31  Meguro.rb #17
        @thinca

自己紹介

  • thinca (Twitter,GitHub他)

  • Rails アプリ書くお仕事をしてます

  • 好きなエディタ: Vim

Vim 使ってる人? 🙋

Vim を拡張するには

  • 自分の .vimrc に設定を書く

  • Vim プラグインを書く

  • Vim script を書く必要がある

    • Vim script わからん

そこで if_ruby

if_ruby とは

  • Ruby interface for Vim

  • Vim script の代わりに Vim で書ける

  • Vim 自体に付いている機能

    • ただしビルド時に有効になっている必要がある

用意するもの

  • +ruby 機能が有効にされた Vim

    • ./configure --enable-rubyinterp
  • brew …は…よく知らない…

if_ruby の実行方法

  1. Vim のコマンドラインから直接

  2. Vim script 内に埋め込む

  3. ファイルに書いて読み込む

1. Vim のコマンドラインから直接

単発で実行する

:ruby print 'Hello Ruby!'

範囲の各行で実行する

:rubydo $_ = "# #{$_}"

2. Vim script 内に埋め込む

2の階乗を計算する例

function! Pow2(num) abort
  ruby << EOR
  res = 2 ** Vim::evaluate('a:num')
  Vim::command(%(let result = #{res.inspect}))
EOR
  return result
endfunction

pow() は Vim script にもあります

3. ファイルに書いて読み込む

sample.rb のようにファイルを用意

class MyVimPlugin
  def useful_feature
    puts 'useful'
  end
end

以下のコマンドでファイルを読み込む

:rubyfile /path/to/sample.rb

注意点

  • 触る対象は結局 Vim

    • Vim の内部の構造をある程度知っている必要がある
  • Vim 内で Ruby の環境は1つだけ

    • トップレベルで定義したメソッドは 当然グローバルでずっと残る

デモ

まとめ

  • Ruby で Vim を拡張できる

    • Vim script を書かなくてよい
    • とは言え Vim のことは知る必要がある
  • +ruby でビルドしてある必要がある

おまけ: 宣伝

  • VimConf 2018 開催します

  • 2018-11-24(Sat) in 秋葉原

  • https://vimconf.org/2018/

  • Vim の作者 Bram 氏が来日します

  • 発表者募集中!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment