4
//console.log('not this, since it is not adjancent')
console.log()
// console.log("uncomment this")
// console.log("uncomment this")  <-- cursor may be at this at this line for instance
// console.log("uncomment this")
// console.log("uncomment this")
// console.log("uncomment this")
console.log()
//console.log('not this, since it is not adjancent')

Is it possible to type some command to uncomment all these adjacent lines of comments without having to count the lines.

I know I can do gc5j, but I don't want to count lines.

4
  • I assume you're referring to the commentary plugin?
    – mattb
    Jun 15, 2022 at 11:08
  • gc5j also works with the tcomment plugin, though gcgc and gcu don’t. tcomment does not seem to have such a feature.
    – Maëlan
    Jun 15, 2022 at 11:33
  • Thanks for your input , but I don't think the plugins you mentioned works with ideavim Jun 15, 2022 at 16:38
  • 1
    Actually, I think the vim-commentary and tcomment plugins are supported in ideavim, see here, under the "commentary" heading.
    – husB
    Jun 16, 2022 at 6:41

2 Answers 2

4

If you're using tpope's commentary plugin, see :help gcgc or :help gcu:

gcgc     Uncomment the current and adjacent commented lines.
gcu

As pointed out by @D. Ben. Knoble, gc is both an operator and a text object! So gc<some motion> will toggle the comment of but dgc will delete the commented block under the cursor and >gc will indent the commented block etc.

5
  • I don't think that specific plugin works with ideavim? Jun 15, 2022 at 16:36
  • @SebastianNielsen I guess not, I haven't used ideavim so I can't be sure
    – mattb
    Jun 15, 2022 at 16:55
  • 1
    @SebastianNielsen It seems to me that commentary plugin is supported (or at least emulated) in ideavim, see here
    – husB
    Jun 16, 2022 at 6:40
  • 1
    tpope's plugin also provides gc as a text object, so dgc and similar become possible
    – D. Ben Knoble
    Jun 16, 2022 at 16:28
  • Yes it's a very nice feature: I use >gc quite often to further indent a comment block
    – mattb
    Jun 16, 2022 at 16:57
2

The way I do this is by creating a folding rule for the comments in the syntax file. Then you can operate on all the folded lines. Another useful technique is to select the lines in visual mode then apply your operation.

2
  • Is it possible to create a macro for all that, so that you can accomplish it in one short command, e.g. gcu? Jun 16, 2022 at 11:04
  • Operating on things in visual mode is a generally useful technique, and could also be used for commenting out code. If you want a simple shortcut you should use the other answer probably. If I were to write a macro, I'd search backwards and forwards for the comment pattern and substitute on the lines you found. Jun 16, 2022 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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