IntelliJ中如何去除块注释星号前的前置空格?
Hey, I’ve run into this exact issue before with IntelliJ and Checkstyle—super frustrating when the IDE’s auto-formatting clashes with team rules! Let’s fix this step by step:
Fixing the Block Comment Space Issue
Step 1: Open IntelliJ’s Settings
Use the quick shortcut to pull up settings:
- Windows/Linux:
Ctrl+Alt+S - Mac:
Cmd+,
Step 2: Navigate to Code Style Settings for Your Language
Expand Editor > Code Style, then select the language you’re working with (e.g., Java, Kotlin, Python—we’ll use Java as an example here).
Step 3: Adjust Block Comment Formatting
Switch to the Code Generation tab, then look for the Comments section:
- Uncheck the box labeled Add space at start of comment line—this is the setting that adds that annoying extra space before the asterisk on subsequent lines.
- Make sure Block comment at first column is checked (this ensures the entire comment block’s border asterisks are flush left, matching your team’s shared header规范).
Step 4: Save and Test
Click Apply then OK to save changes. Now when you generate a block comment (e.g., by typing /** and pressing Enter, or using the auto-generate header feature), it’ll look exactly like your team requires:
/*************************************************************************** * This is a code header. * No extra space before the asterisk now! ***************************************************************************/
Bonus: Custom File Header Templates
If your team uses a standardized file header (like class-level comments), double-check your template settings:
- Go to Editor > File and Code Templates
- Select the Includes tab, then edit the File Header template
- Ensure any line starting with an asterisk doesn’t have a leading space. For example:
/** * ${DESCRIPTION} * * @author ${USER} * @since ${DATE} */
Pro Tip for Team Consistency
You can export your IntelliJ code style settings as an XML file and share it with your team:
- In the settings panel, click the gear icon in the top-right corner
- Select Export Scheme > IntelliJ IDEA code style XML
- Send this file to teammates, who can import it via Import Scheme to match your settings exactly.
内容的提问来源于stack exchange,提问作者theMayer

