Skip to content

MD047 - End Files with a Single Newline

Aliases: single-trailing-newline

What this rule does

Ensures every file ends with exactly one newline character, following standard text file conventions.

Why this matters

  • Version control: Git and other tools expect files to end with newlines
  • File concatenation: Prevents issues when combining files
  • Terminal display: Files display correctly in command-line tools
  • Cross-platform compatibility: Consistent behavior across operating systems

Examples

✅ Correct

# My Document

This is the content of my document.

The last line ends with a single newline.

(Note: There's an invisible newline after "newline.")

❌ Incorrect

# My Document

This is the content of my document.

The last line has no newline.

(Note: In the actual file, there would be no newline after "newline." - the file ends immediately)

# My Document  

This is the content of my document.

The last line has multiple newlines.

(Note: Multiple blank lines at the end)

🔧 Fixed

# My Document

This is the content of my document.

The last line now ends with a single newline.

(Note: Exactly one newline added at the end)

Configuration

This rule has no configuration options.

Automatic fixes

When enabled, this rule will:

  • Add a newline if the file doesn't end with one
  • Remove extra newlines if there are multiple
  • Ensure exactly one newline at the end

Why this matters technically

  • Many text editors automatically add this newline
  • POSIX defines a line as ending with a newline
  • Prevents "No newline at end of file" warnings
  • Makes file processing more predictable

Special cases

  • Empty files are considered valid
  • Binary files are not checked
  • The rule only checks for a single newline, not multiple blank lines (see MD012)

Learn more

  • MD012 - Remove multiple consecutive blank lines
  • MD022 - Add blank lines around headings
  • MD031 - Add blank lines around code blocks