Comment/uncomment a block of code in NetBeans shortcut

JavaNetbeans

Java Problem Overview


Is there a shortcut in NetBeans to highlight a block of code and comment/uncomment it?

Java Solutions


Solution 1 - Java

Try this combination in the Netbeans Editor: ctrl + shift + c

Solution 2 - Java

The list of keyboard shortcuts can be found in the NetBeans Wiki.

Turn On/Off Search Result highlights

Alt + Shift + H

Add/remove comment. Should work for all languages

Ctrl + / or in mac āŒ˜ + /

Solution 3 - Java

An IDE independent trick (that works for all languages in the C/Java/C++/C# family) I found to comment/uncomment blocks of code fast is the following:

int a = 5;
int b = 2;
//*
if(a < b) {
    int t = a;
    a = b;
    b = t;
}
//*/
System.out.println("a: "+a);

Say you want to comment and uncomment the if block frequently. You can use the //* and //*/ markers. You comment the block by removing one / in the //* part. Thus:

int a = 5;
int b = 2;
/*
if(a < b) {
    int t = a;
    a = b;
    b = t;
}
//*/
System.out.println("a: "+a);

Why this works

In case the first line reads //*, it is interpreted as // *, thus you comment the * and don't comment the remainder of the block. The //*/ is ignored as well since it is interpreted as // */.

In case the first line reads /*, it is interpreted as the start of a comment block. Java searches for the corresponding end which is // */ (the // is ignored).

Solution 4 - Java

In mac, it is more stable to use command+shift+c. Sometimes, command+/ could be usable but not so stable.

Solution 5 - Java

Also, to have a whole block commented, can be useful the rectangular selection trick as a single "null column" rectangle where we can add any comment character we like (eg. an hash or a slash):

enter image description here

The shortcut in Mac is Ctrl+Shift+R as explained in this thread.

To return to the normal selection just repeat the same shortcut.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmrblahView Question on Stackoverflow
Solution 1 - JavaGirishView Answer on Stackoverflow
Solution 2 - JavalofteView Answer on Stackoverflow
Solution 3 - JavaWillem Van OnsemView Answer on Stackoverflow
Solution 4 - JavahtlbydgodView Answer on Stackoverflow
Solution 5 - JavaRikiRiocmaView Answer on Stackoverflow