11

I’d like to select (or delete, or change, or …) the whole block of commentary under my cursor.

If my cursor is at the beginning of the commentary block (i.e. over the opening comment character), and it’s a type of comment block with discrete start/end markers, then I can select the entire comment block with V%, but I don’t know of a movement to move to the first character of the block currently under the cursor.

Better yet would be an inner-text object for comments.

Does anybody know of any of the above, or how I could easily create the latter? (I’m new to vim as a whole.)

9

Assuming you're working with C++ block comments, these commands should do what you want:

  1. [/
  2. v
  3. ]/

These will:

  1. The first will jump to the start of a block comment (the slash in /*)
  2. The second will turn on select mode
  3. The third will jump to the end of a block comment (the slash in */), which selects the entire comment block.

I hope that helps!

EDIT: One note about using the lower case v instead of the upper case V. The upper case V will select entire lines while the lower case v will only select the text from the cursor's start position to the end position. Using the lower case v also means not having to hit the shift key. :)

  • 2
    [ and ]. Two more incredibly useful nav commands for my vim vocabulary, to go alongside f and t :) Thanks – Mark K Cowan Feb 20 '15 at 9:07
  • You can also use % for the 3. step, which is easier to type in many keyboards. – 12431234123412341234123 Aug 30 '16 at 8:39
  • I am trying to use this in my .vimrc (["v]") but it does not work. Do I miss something? – nocibambi Sep 2 at 14:35

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.