vim-commentary icon indicating copy to clipboard operation
vim-commentary copied to clipboard

Comment out a portion of a line

Open adamm opened this issue 12 years ago • 9 comments

The vim-commentary documentation states three keyboard mapping examples to comment out whole lines of code, but I'm curious there is any plan to extending this plugin to support portions of a line.

Such as, for example, "\iw" would comment out only single word, and "{Visual}" comment out only highlighted words.

I can attempt to tackle this feature myself, but I'm no Tim Pope when it comes to Vim plugins :)

adamm avatar Mar 10 '12 20:03 adamm

Oops, Github escaped by backslashes :) .. Obviously I meant "\iw" and "{Visual}\" respectively.

adamm avatar Mar 10 '12 20:03 adamm

I have no such plans. How often does this really come up? I'm trying to think of even one instance of wanting this.

tpope avatar Mar 10 '12 20:03 tpope

Mind you, I'm biased because like 90 percent of my code is in languages that only have linewise comments. But that raises the question of what happens in those languages.

tpope avatar Mar 10 '12 20:03 tpope

Certainly language requirements dictate comment structure, but also personal coding style can avoid the need for partial line comments. To give you a specific example in C++, I was debugging a remote problem yesterday with the following block:

if (!first_run_ui_bypass && !force_first_run && !headless_setup &&
    (parsed_command_line.HasSwitch(switches::kApp) ||
     parsed_command_line.HasSwitch(switches::kAppId) ||
     parsed_command_line.HasSwitch(switches::kNoFirstRun)))
  first_run_ui_bypass = true;

I needed to bypass out the first two boolean checks, while maintaining the essence if block and bracket syntax. To do so, I was hoping to highlight only part of the first line, press "\", and produce this:

if (/*!first_run_ui_bypass && !force_first_run &&*/ !headless_setup &&
    (parsed_command_line.HasSwitch(switches::kApp) ||
     parsed_command_line.HasSwitch(switches::kAppId) ||
     parsed_command_line.HasSwitch(switches::kNoFirstRun)))
  first_run_ui_bypass = true;

Instead got this:

/* if (!first_run_ui_bypass && !force_first_run && !headless_setup && */
    (parsed_command_line.HasSwitch(switches::kApp) ||
     parsed_command_line.HasSwitch(switches::kAppId) ||
     parsed_command_line.HasSwitch(switches::kNoFirstRun)))
  first_run_ui_bypass = true;

Naturally I can fix this by hand, but then there's no benefit to using this plugin ;)

adamm avatar Mar 10 '12 21:03 adamm

Okay, well, I guess I'm not opposed to it, but I'm not very motivated to work on it either. And I'm kind of worried about how much complexity it will introduce.

One thing I am absolutely opposed to is configuration options for separate linewise and inline comments, because this means one of:

  • The user has to maintain a bunch of comment configuration options
  • The plugin has to maintain a bunch of comment configuration options

Both of those are unacceptable, in my book. Instead, we either have to parse 'comments', or limit support to a single comment syntax per file type.

This is a daunting task, so I feel like I should also inform you of tcomment, a commenting plugin that does pretty much everything.

tpope avatar Mar 15 '12 15:03 tpope

Is there any way to comment a block instead of comment each line in the block? for example, comment like this:

/*
int x = 3;
int y = 4;
*/
return 0;

instead of:

/* int x = 3; */
/* int y = 4; */
return 0;

Alaya-in-Matrix avatar Nov 29 '14 16:11 Alaya-in-Matrix

@Alaya-in-Matrix I think a separate issue is appropriate for a request like that (my comment on it is -- how might user choose which type of comment to apply? global config?)

And +1 for this issue. I often find a need to comment out a specific argument to a function call. It's very natural with argtextobj plugin: vaagc should comment out the arg under cursor.

unphased avatar Jun 04 '15 03:06 unphased

Also would be interested in this, but I also understand that it wouldn't be very simple to develop and maintain

edi9999 avatar Aug 19 '16 14:08 edi9999

sounds like you could do this with vim-surround instead

grepsuzette avatar Sep 08 '23 02:09 grepsuzette