5

Is it possible using NERD-commenter to uncomment a multiline block under the cursor without selecting the entire block visually first?

If no, are there any better plugins, that can do the job?

Here is an example:

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.1</version>
    </dependency>
</dependencies>

Commenting out first dependency tag using leadercm I get

<dependencies>
    <!--<dependency>
        <groupId>org.jboss.logmanager</groupId>
    |   <artifactId>log4j-jboss-logmanager</artifactId>
        <version>1.1.1.Final-redhat-1</version>
        <scope>provided</scope>
    </dependency>-->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.1</version>
    </dependency>
</dependencies>

Now I place the cursor inside the commented out tag (like showed above) and want to uncomment the tag.

  • leadercu - does nothing
  • leadercspace - comments out the current line breaking XML format.
  • 2
    Just to be clear: what bothers you is that you have to visually select the commented text and it is not easy to do? Because if that's the problem one solution could be to install vim-textobj-user and vim-textobj-comment so you can select your commented text with vaC and then uncomment it with <leader>cu – statox Nov 21 '16 at 14:10
  • @statox You are 100% right. I will test your solution. Btw. please, post an answer, so I can upvote it :) – Boris Brodski Nov 21 '16 at 15:47
  • I posted this as an answer, if you are fully satisfied with this solution you can consider marking the answer as accepted (the green button under the downvote button next to the answer) this way future users with the same problem will know that this solution worked for you. If you're not satisfied don't hesitate to give more details in the comment so we can see how to improve it. – statox Nov 22 '16 at 9:02
  • 1
    @statox sure, I wanted to test it first. It works pretty well, Thank you! – Boris Brodski Nov 22 '16 at 9:29
3

So the problem here is to easily select the commented text. Vim doesn't have a built-in way to select a portion of commented text.

A possible solution is to create a new text object which will represent a chunck of commented code. To do so you can:

  • Define it manually which can be a pretty laborious work if you're not used to vimscript (even if you are actually)
  • Or you can use the plugin vim-textobj-user which allows the user to easily create new text objects. With the plugin you can install the vim-textobj-comment plugin which uses vim-textobj-user to create the text object we need.

Once it is installed you can simply use vaC to select the commented text and then use the NERDCommenter command leadercu to uncomment your text.

  • Cool combination of plugins! I use it with nmap <silent> <leader>u vaC<leader>cu – Boris Brodski Nov 22 '16 at 9:30

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.