MD009 - No Trailing Spaces
This rule checks for trailing spaces at the end of lines.
Why This Rule Exists
Trailing spaces are usually unintentional and can cause issues:
- They're invisible in most editors, making them hard to spot
- They can cause unexpected behavior in version control systems
- They may render differently across different markdown processors
- They increase file size unnecessarily
Examples
❌ Incorrect (violates rule)
This line has trailing spaces ← spaces
This one has a tab at the end ← tab
Multiple spaces here ← spaces
(Where arrows indicate invisible whitespace characters)
✅ Correct
This line has no trailing spaces
This one is clean too
Two spaces for line break are allowed
when configured (br_spaces = 2)
Configuration
[MD009]
br_spaces = 2 # Number of trailing spaces allowed for line breaks (default: 2)
strict = false # If true, disallow even configured line break spaces (default: false)
Automatic Fix
This rule supports automatic fixing. The fix will:
- Remove all trailing whitespace from lines
- Preserve configured line break spaces (typically 2 spaces)
- Maintain the line's content and structure
When to Disable
Consider disabling this rule if:
- Your project intentionally uses trailing spaces for formatting
Rule Details
- Rule ID: MD009
- Aliases: no-trailing-spaces
- Category: Whitespace
- Severity: Warning
- Automatic Fix: ✅ Available
Configuration Options
[MD009]
br_spaces = 2 # Number of spaces allowed for line breaks (default: 2)
strict = false # If true, disallow even configured line break spaces (default: false)
Understanding Line Breaks
Markdown supports two types of line breaks:
Hard Line Break (Two Spaces)
First line
Second line on new line
Renders as:
First line
Second line on new line
Paragraph Break (Blank Line)
First paragraph
Second paragraph
Renders as:
First paragraph
Second paragraph
Common Issues and Solutions
Invisible Trailing Spaces
Problem: Spaces at line ends are invisible in most editors.
This line has spaces ← invisible spaces
Another line with tab ← invisible tab
Solution: Configure your editor to show whitespace or use the automatic fix.
This line has no trailing spaces
Another clean line
Inconsistent Line Break Handling
Problem: Different markdown processors handle trailing spaces differently.
Some text
More text
Final text
Solution: Use exactly two spaces for line breaks when needed.
Some text
More text
Final text
Editor Configuration
Visual Studio Code
Add to settings.json
:
{
"files.trimTrailingWhitespace": true,
"markdown.preview.breaks": true,
"[markdown]": {
"files.trimTrailingWhitespace": false
}
}
Vim
Add to .vimrc
:
" Show trailing spaces
set list listchars=trail:·
" Remove trailing spaces on save
autocmd BufWritePre * %s/\s\+$//e
Sublime Text
Add to preferences:
{
"trim_trailing_white_space_on_save": true,
"draw_white_space": ["all"]
}
Version Control Considerations
Git Configuration
Configure Git to warn about trailing whitespace:
git config core.whitespace trailing-space
Pre-commit Hooks
Use a pre-commit hook to catch trailing spaces:
#!/bin/sh
# .git/hooks/pre-commit
exec git diff --check --cached
When Line Break Spaces Are Needed
Sometimes two trailing spaces are intentional:
Poetry and Verses
Roses are red
Violets are blue
Markdown is great
And so are you
Addresses
123 Main Street
Suite 100
Anytown, ST 12345
Preserving Formatting
Name: John Doe
Email: john@example.com
Phone: 555-1234
Performance Impact
Trailing spaces can impact:
- File Size: Unnecessary whitespace increases file size
- Diff Noise: Changes to trailing spaces clutter version control
- Search/Replace: Invisible characters can break patterns
- Copy/Paste: Trailing spaces may cause unexpected behavior
Automatic Fix Behavior
The automatic fix will:
- Remove all trailing whitespace from each line
- Preserve exactly
br_spaces
spaces when configured (default: 2) - Handle tabs and mixed whitespace
- Maintain line endings (LF/CRLF)
Fix Examples
Before:
Text with spaces
Text with tab
Text with mixed
After (default config):
Text with spaces
Text with tab
Text with mixed
After (br_spaces=2, preserving line breaks):
Paragraph text
Line break needed
Next line
Related Rules
- MD010 - Hard tabs
- MD012 - Multiple consecutive blank lines
- MD047 - Files should end with a single newline