Skip to content

Instantly share code, notes, and snippets.

@ujihisa
Last active December 1, 2016 20:03
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 ujihisa/4662b4a69bc09d9ed41daf32adabdedd to your computer and use it in GitHub Desktop.
Save ujihisa/4662b4a69bc09d9ed41daf32adabdedd to your computer and use it in GitHub Desktop.

Vim

この記事はVim Advent Calendar 2016の1日目のエントリです。(同名のAdvent calendarが2つあるのにご注意ください。もう一つは http://qiita.com/advent-calendar/2016/vimです)

Vim + Ruby

著者のujihisaはCanadaのVancouverにあるHootsuiteという会社で6年半ほど働いていて、そこでは主にScalaを書くなどしていました。そのHootsuiteを退職し、先月日本の東京に引越しをし、その後就職活動を行った結果、東京にあるFablicという会社に2017年から勤務するはこびとなりました。 そこでは主にRubyを使うことになるので、Rubyのための開発環境をまとめます。

Fril

前提として、著者はHootsuiteで働く前は主にRubyやHaskellを用いていたものの、Hootsuite在籍中は小さいスクリプトやMinecraft plugin作りに軽くRubyの短いコードを書く程度で、大規模なコードベースに触れる機会はありませんでした。

Hootsuite

余談ながら、去年のVim Advent Calendar 2015Vim script Advent Calendar 2015の記事タイトル一蘭を見たところ、去年はVimとRubyの話題について言及した記事はないようでした。

vim-ruby

https://github.com/vim-ruby/vim-ruby

VimによるRubyのための開発環境をまとめるというのは大嘘で、今回はまず基本に立ち返り、配布されているVimに同封されているvim-rubyを詳しくみてみましょう。 vim-rubyですでに提供されている機能については特に別個のプラギンを導入する必要がないわけで、まずこれをきちんと把握しておくことが大切です。

コマンド

プラギン名がvim-rubyなので、まずは素直に:h rubyを見てみましょう。

:rub[y] {cmd}		Execute Ruby command {cmd}.  A command to try it out: >
				:ruby print "Hello"

+rubyなVimで使うあれです。どちらかというとプラギン製作者向けで、普段Vimを使うときにはこれよりもっと高レイヤであるquickrunを使いましょう。

ほかに:rub[y] <<, :[range]rubyd[o], :rubyf[ile]などがありますが、同じ扱いです。

:h vim-ruby

なんかおかしいなとは思っていたのですが、実は:h rubyはvim-rubyのhelpではなく+rubyのものでした。まあ当たり前か。vim-rubyのhelpは:h vim-rubyを見ましょう。

motion

]mなどといった、]/[m/Mの組み合わせによる2キーのmotionがいくつか与えられています。 mのメソッドと角括弧のクラスと覚えておきましょう。

といいつつ実際には一つ次のメソッドとかにジャンプするのではなく、特定のメソッドにいきなり飛びたいときが多いので、unite-outlineの出番の方が多くなりそうな気もします。

indent

g:ruby_indent_access_modifier_styleをいじることで、コードベース内のRubyのインデントの各慣習に対応できるようになっています。

Access modifier style "normal":

  class Indent
    private :method
    protected :method
    private
    def method; end
    protected
    def method; end
    public
    def method; end
  end

Access modifier style "indent":

  class Indent
    private :method
    protected :method
    private
      def method; end
    protected
      def method; end
    public
    def method; end
  end

Access modifier style "outdent":

  class Indent
    private :method
    protected :method
  private
    def method; end
  protected
    def method; end
  public
    def method; end
  end

同様に、g:ruby_indent_block_styleもあります。

Block indent style "expression":

  first
    .second do |x|
    something
  end
<
Block indent style "do":
>
  first
    .second do |x|
      something
    end

.を行頭にもってくるのが気持ち悪く感じますが、デフォルトの"expression"はかなり気持ち悪いインデントになるので、~/.vimrc`で

let g:ruby_indent_block_style = 'do'

すると便利ということがわかります。

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