4

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.

Example:

some_variable_name = begin
                       42
                     end

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.

1
  • i'm so thankful you asked this question
    – rthbound
    Commented Dec 28, 2022 at 1:16

1 Answer 1

6

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'
endif

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

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 <[email protected]>

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'
endif

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
  42
end 

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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