Syntax Hack – single-character block comment toggles

tl:dr; Use //*/ to terminate block comments, then use /* and //* to toggle the block in and out of code.

So this is a small trick but it’s really useful — a syntax hack at the same kind of level of a keyboard shortcut.

Most C-syntax langauges use line comments and block comments like so;

// line comment

block comment

Block comments mask more code, but they tend to be ‘fiddly’ to put into your code and take out. This can be a pain when you have a block you’re developing and you want to comment it in and out regularly;

... big code block here
... which goes on for a while
... and you want to toggle it
... in and out regularly.

So if you change your terminating symbol to //*/, an interesting thing happens; it functions as the terminator of a block comment inside a block comment, but a line comment otherwise. This means you can toggle code in and out by putting a single extra slash on the start of the block comment;

code here commented out


code here is now active

So there is it. A single-character togle of a block comment.

2 thoughts on “Syntax Hack – single-character block comment toggles

  1. You can also toggle between *two* blocks of code like this:

    code here is now active
    code here is inactive

    Delete the first slash to toggle the active code to the second section of code. Great for switching between production code and experimental new code as you test a bug fix or new feature.

    • That is great! Hadn’t thought of that, but the ‘swinging’ nature of ‘/*/’ is a very clever trick.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s