Context is Ruby. Lets say I have a long line which has a block which I want to have simply in multiple lines:

let:(variable) { do.some.stuff(expressed_as_a_long_line { x }) }

I'd like split this into multiple lines like this

let:(variable) {
  do.some.stuff(expressed_as_a_long_line { x }) 

...and later maybe even:

let:(variable) {
    expressed_as_a_long_line { x }

Is there any simple inbuilt way or plugin for similar stuff?


The plugin splitjoin.vim is designed to do this and has support for ruby. https://github.com/AndrewRadev/splitjoin.vim

For a vanilla solution:

function! OpenCode()
    call searchpair('{\|(', '', ')\|}', 'bW')
    exe 'normal!' "%i\<c-m>\<esc>%a\<c-m>\<esc>\<c-o>m]"
    normal! '[=']
    let l:save_slash = @/
    let @/ = l:save_slash
nnoremap <c-b> :<c-u>call OpenCode()<cr>

Pressing Ctrl-B when inside a block will open it up.

  • splitjoin.vim is very cool, only that it forces multiline curly-braced blocks into do-end, which is something i would like to control. Maybe it can be configured? And vanilla solution is also very cool, only that it leaves extra space at the end of the line. I don't know the syntax enough to understand how to control that either. Thank you for your answer, I'm bit closer than before :) – Taavi Ilves Oct 17 '17 at 8:58
  • 1
    I think it would be better to spell out all the characters that need to be entered literally. I don't think "etc." will be specific enough for many (most?) readers, particularly with regards to the escape character. – Rich Oct 17 '17 at 14:04
  • 1
    TaaviIlves, I don't use the plugin (or ruby) myself, but I think it is configurable and actively developed. Added code to remove trailing whitespace. A bit uglier but it should work. @Rich you're right- I moved to a copy-paste-able solution. – Mass Oct 17 '17 at 15:39
  • That's not what I meant, but it's even better! – Rich Oct 17 '17 at 15:40
  • Thank you Rich! I leave it currently open for any other solution, but will mark it a solution, when noone replies. – Taavi Ilves Oct 17 '17 at 16:49

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.