What are these M's that keep showing up in my files in emacs?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you see ^M markers in Emacs, you are usually seeing carriage return bytes from Windows-style line endings. This is a newline format mismatch, not random file corruption. The long-term fix is to align Emacs buffer encoding, Git normalization rules, and team repository policies.
What ^M Means
Different systems represent line endings differently:
- Unix-style uses LF
- Windows-style uses CRLF
When a file containing CRLF is displayed in contexts expecting LF, Emacs may show the CR byte as ^M.
This indicator points to formatting mismatch. Your text is generally intact, but the newline bytes differ from expected style.
Check Current Buffer Line Ending in Emacs
Before converting anything, inspect the buffer coding system.
Interactive methods:
- run
M-x describe-coding-system - inspect
buffer-file-coding-system - look for variants with DOS line endings
This confirms whether the file currently uses CRLF or LF.
Convert Current File to Unix Line Endings
To remove visible ^M markers for Unix-style projects, set Unix file coding and save.
Interactive equivalent:
M-x set-buffer-file-coding-system- select
unix - save the buffer
This converts line endings in that file from CRLF to LF.
Set Better Emacs Defaults
If your projects mostly use LF, set default coding in your Emacs configuration.
This helps new files start in LF format and reduces recurring newline churn.
Still, editor defaults alone are not enough for teams. Repository-level policy should enforce line endings consistently across platforms.
Use Git Attributes to Prevent Recurrence
Add .gitattributes rules so line ending normalization is explicit.
Then normalize existing tracked files once:
This keeps cross-platform contributions consistent and prevents repeated ^M reintroduction.
Mixed-Platform Strategy
Some repositories need both LF and CRLF:
- scripts for Unix environments should use LF
- batch files for Windows often need CRLF
The correct strategy is explicit per-file-type rules, not forcing one global format blindly.
When onboarding new contributors, document line-ending expectations in CONTRIBUTING.md along with editor recommendations.
External Conversion Tools
Sometimes batch conversion is easier outside Emacs, especially for one-time cleanup.
Alternative command:
After conversion, review diffs carefully and keep newline-only changes in dedicated commits.
Troubleshooting Persistent Reappearance
If ^M keeps coming back, check in this order:
- repository
.gitattributesis present and committed - local Git settings are not conflicting
- generators or scripts are not rewriting files as CRLF
- CI or formatting tools are not reapplying platform defaults
This process usually identifies the source quickly.
CI and Automation Guardrails
To stop recurring issues, include line-ending checks in CI for critical file types.
Automation prevents format regressions from reaching main branches.
Common Pitfalls
A common pitfall is manually deleting visible ^M characters in the editor without fixing file encoding and repository policy.
Another pitfall is applying global LF conversion to files that legitimately require CRLF, such as Windows batch scripts.
A third pitfall is mixing functional edits with large normalization changes, which makes code review difficult.
Teams also overlook auto-generated files and build tools that keep reintroducing unwanted line endings.
Summary
^Min Emacs usually indicates CRLF line endings shown in an LF-oriented workflow.- Convert file coding in Emacs to Unix style when LF is desired.
- Enforce repository policy with
.gitattributesfor durable cross-platform consistency. - Allow explicit CRLF exceptions for file types that require it.
- Add CI checks and isolated normalization commits to prevent repeat churn.

