Turning code formatting off and on in Eclipse

Eclipse allows you to turn code formatting on and off for a certain section of your code. This is extremely helpful when, say, you want to temporarily comment out a block of code and you end up formatting the file. Eclipse will have mashed up the whole comment block into an unrecognizable mish-mash.

To avoid this, you can surround that code (or comment) block with the following:


// @formatter:off


your code block


// @formatter:on



Remember, for this to work, you have to enable these tags in the code formatting preferences.

In Eclipse preferences: Java > Code Style > Formatter. Click on “Edit” button, “Off/On Tags”, check off “Enable Off/On tags”.

You can even select the text you want, the default being “@formatter:off” and “@formatter:on”.

Code on!

Leave a comment