Search code examples
vimneovim

Vim select partially multiple lines in block mode where the targets are variable in size


Imagine you have this kotlin code:

package sample

data class Sample(
    val title: String,
    val subTitle: String,
    val threeBlocksTitle: String
)

You are editing with vim, and you want to make V-BLOCK selection like this one made with Intellij:

Intellij sample

Everything I did try result in one of these two scenarios:

select too much:

select too much

select too few:

select too few

I am struggling with that and I could not figure out how to manage to do it :S

Edited

My final purpose is to copy the val names and create an instance using it as named args, like that:

named args


Solution

  • First of all, for visual selection:

    If you want vim to display the visual block exactly as what IntelliJ shows, you cannot do it.

    However, if you want to yank only the variable names, it can be certainly done by vim.

    In my example codes, I am using the register x, so please clear it before trying the solution.

    clear the x register:

    qxq
    

    What you can do is, select the 3 lines containing the target variable names, and do:

    :'<,'>s/\w\+\ze:/\=setreg('X', submatch(0), 'V')/gn
    

    Then go to the place you want to paste:

    "xp
    

    enter image description here

    Note that, when you pressed : the range will be automatically added by vim, so you don't have to type

    In fact, if I were you, I would copy the 3 lines (whole line or without the val), and paste somewhere, it is pretty easy to edit to achieve your goal:

    enter image description here