2

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

ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • 1
    I guess the selection is not the final goal. what would you want to do after the target is selected? – Kent May 22 '20 at 15:21
  • @Kent that is correct, usually I do it for different purposes, in this specific case I want to copy the val names to paste in another place to create a new instance of the obj with named args, I am adding a screenshot of the final purpose :) – ademar111190 May 22 '20 at 16:26
  • thanksd, I am about to post the solution. – Kent May 22 '20 at 16:43

3 Answers3

3

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

Kent
  • 189,393
  • 32
  • 233
  • 301
  • I am impressed, to that specific case the 2 way is far better, but sometimes the first way will be the most usefull. Thank you very much! – ademar111190 May 22 '20 at 18:03
  • @ademar111190 yes, it depends on what do you want to do. You're welcome. – Kent May 22 '20 at 22:42
1

I suggest break task in steps. First, align lines you need. It can be achieved with many ways. I use Tabular plugin. Select lines with V and do:

'<,'>Tabularize/:

Now you have this:

val title            : String,
val subTitle         : String,
val threeBlocksTitle : String
val title2           : String,

Second step is block select with <C-V>.

block selection

Further steps depend on your goal. You probably want to delete spaces at the end. Put text somewhere and use simple regex like:

'[,']s; ;;g

Another approach with macro.

Clean register a (for example) and set it to initial value:

:let @a="\n"

Navigate to first variable and record macro to register q:

qq"Ayiwjq

It will yank word (variable), append it to register a and move one line below. After that you can use @q on each variable you want add to result. iw part can be changes for t: if non-word characters may appear.

Dmitry
  • 436
  • 2
  • 7
  • I won't go in this way. 1) require extra plugins 2) you have to handle the spaces within your selection. 3) you've changed the original text, you have to undo after yanking, but it is error-prone – Kent May 22 '20 at 17:05
  • It is an interesting approach, but I agree with the arguments of @Kent – ademar111190 May 22 '20 at 18:04
  • Changed original text to show layout of block-select. Did not understand about "you have to undo after yanking", what do you mean? BTW, you can also use macro without any plugins, I will add it a bit later maybe. – Dmitry May 22 '20 at 19:39
  • 1
    @Dmitry the undo is to back the `val title : String,` into `val title : String,` because you don't want to change that code, you just want to copy – ademar111190 May 22 '20 at 20:15
0

I know I'm a bit late, but coming from IntelliJ I was just having the same question. If you are open to using plugins, you can check out vim-visual-multi. This plugin has its own help, which you can open using :help visual-multi.

Once installed, put your cursor on the t of title and press <CTRL-Down> twice; this will select the three initial letters of your variable names and put you in a mode called cursor mode (:help vm-cursor-mode, you can exit it with <ESC>). Then, press yw to yank the word for each cursor. After doing so, go to the place where you want to paste your selection; there you just press p, P, zp, or zP, whichever you like.

enter image description here

If you prefer having more visual clarity before yanking, you can switch to extend mode (:help vm-extend-mode). This mode is similar to visual mode and highlights the selection you make. You can switch to it by pressing <TAB> after selecting the three initial letters. Once in this mode, press t: to highlight everything up to the : and then press y to yank it.

enter image description here

Alex R
  • 3,139
  • 1
  • 18
  • 28