Explain Codes LogoExplain Codes Logo

Eclipse: Set maximum line length for auto formatting?

java
editor-preferences
auto-formatting
custom-formatter-profile
Alex KataevbyAlex Kataev·Jan 24, 2025
TLDR

Configure Eclipse's auto-formatting line width via Preferences > Java > Code Style > Formatter. Modify the current profile's Line Wrapping settings and tweak the Maximum line width. You can use your desired value, for instance 80 characters:

// Winter is coming, better wrap this line to 80 characters formatterProfile.setLineWidth(80);

Press Ctrl+Shift+F to format your code, your lines will now wrap according to the new limit.

The Line Wrapping tab isn't the only place where you'll apply changes. The Comments tab allows for a unique maximum width setting, ensuring that your code comments are as readable as possible. Creating a new profile is an alternative if you don't want to modify Eclipse's built-in profiles.

In-depth Look at Configurations

Creating a Custom Formatter Profile

In case you're utilizing one of Eclipse's built-in formatter profiles, generate a new profile to save your custom adjustments. Simply hit New..., name your new profile, and tweak away.

Configuring Comment Line Width

Your Java code and comments don't necessarily have the same readability requirements. So, emphasizing on separately setting their line widths is essential. Navigate to the Comments tab to adjust comment line width.

In addition to the Java Formatter, manipulating width lengths for XML, HTML, and other file types is feasible through their particular Editor Preferences.

Deeper Dive in auto formatting

Customizing Line Width for Non-Java Files

It's worth mentioning that Eclipse caters to file types beyond Java such as HTML, PHP, JSP, and JSPF. Simply head over to their corresponding Editor Preferences to alter formatting preferences.

Relevance of Eclipse Versions

Each release of Eclipse from Helios through current releases may have slight differences in settings navigation. Hence, double-check the documentation relevant to your specific version.

Enlisting Visual Aids

Refer to visual aids like snapshots or detailed GIFs from tutorial websites or Eclipse's official documentation when making adjustments. They provide an additional layer of understanding.