Is there a shortcut in NetBeans to highlight a block of code and comment/uncomment it?
5 Answers
Try this combination in the Netbeans Editor: ctrl + shift + c

- 11,907
- 3
- 34
- 51
-
or else ctrl + / – Bunny Joel May 23 '18 at 08:18
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 ⌘ + /

- 5,891
- 10
- 63
- 97

- 864
- 7
- 6
-
8Can't get Ctrl + / to work on a swedish keyboard, worked with Girish answer. – Henrik Lindgren Mar 30 '14 at 12:26
-
11netbeans only comments by line `//`. How can you do block comments? `/* */` – t q Sep 29 '16 at 14:09
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).

- 443,496
- 30
- 428
- 555
-
5I use a similar trick for switching between two chunks of code. `/**/` at the start, `/*/` between the two chunks, and `/**/` at the end. Simply remove or re-add one of the asterisks from the first comment to switch between blocks. – Samah Mar 08 '15 at 01:45
In mac, it is more stable to use command+shift+c. Sometimes, command+/ could be usable but not so stable.

- 330
- 2
- 8
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):
The shortcut in Mac is Ctrl+Shift+R as explained in this thread.
To return to the normal selection just repeat the same shortcut.

- 746
- 12
- 28