1

I have selected a JS block such as following:

function map(xs: string[], cb: Function) : string[] {
  const result: string[] = [];
  for(let i = 0; i++; i < xs.length) {
    result.push(cb(xs[i]));
  }

  return result;
}

Now I want to go to the beginning of each line and comment out each line in the block (inserting // at the beginning of each line). How can I do this in the fastest way possible using text objects? I am aware that this is also possible by doing column selection Ctrl + V, <number>j, I, // and ESC sequence. But I want to do this in the fastest way possible.

0

2 Answers 2

3

You can try this:

vip<Ctrl-v>I//<ESC>
  1. vip select paragraph
  2. <Ctrl-v> make selection blockwise
  3. I// insert // into begining of a selection block

enter image description here

PS: I would suggest to use vim-commentary plugin from Tim Pope where it would be simple gcip.

0

I think the fastest would be using the commentary plugin as Maxim Kim mentioned: just type gc since you already have the lines selected. But if you want another option using vanilla vim, you could try using regex substitution:

:s&^&//

You can use other characters instead of &; I used that one because it's next to ^ on my keyboard. I use it instead of the standard / to avoid having to escape the slashes in the substitution, as in:

:s/^/\/\/

but that also works.

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