When I start a ruby block in the middle of a line, the next line is indented deeper than I'd like. It's indented from the start of the block, not the start of the line.


some_variable_name = begin

I would like line 2 to be indented one level deeper than 1. 3 should match 1.

Some more examples in this screenshot. Same logic applies to the case statement below.


The file that controls this lives at /usr/share/vim/vim80/indent/ruby.vim. Opening the file, I see it has two variables:

if !exists('g:ruby_indent_access_modifier_style')
  " Possible values: "normal", "indent", "outdent"
  let g:ruby_indent_access_modifier_style = 'normal'

if !exists('g:ruby_indent_block_style')
  " Possible values: "expression", "do"
  let g:ruby_indent_block_style = 'expression'

Unfortunately there is no documentation for this at all; either in :help ft-ruby-syntax or in the file. I tried playing with both, to no avail.

However, at the top of the file there is some author information:

" Language:             Ruby
" Maintainer:           Nikolai Weibull <now at bitwi.se>
" URL:                  https://github.com/vim-ruby/vim-ruby
" Release Coordinator:  Doug Kearns <dougkearns@gmail.com>

At that page GitHub, I see a newer version of indent/ruby.vim which has a new option:

if !exists('g:ruby_indent_assignment_style')
  " Possible values: "variable", "hanging"
  let g:ruby_indent_assignment_style = 'hanging'

Aha! This looks promising! I saved that file to ~/.vim/indent/ruby.vim, added let g:ruby_indent_assignment_style = 'variable' to my vimrc, restarted Vim, and now I get:

some_variable_name = begin

Which seems to be what you want!

Note that instead of downloading this one file, you may want to clone the entire project and load it as a package with some package manager or Vim's package feature. Unfortunately Vim's runtime files tend to lag a bit behind upstream.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.