Emacs: Newline Convention CR LF

By Xah Lee. Date: . Last updated: .

Characters for Newline

Different operating systems use different characters for newline (aka end-of-line character) .

Newline is indicated by 1 or 2 control characters.

Control Characters for Newline
NameAbbrevDecimal
Codepoint
Escape
Notation
Caret
Notation
Emacs Input method
Line FeedLF10\n^JCtrl+q Ctrl+j
Carriage ReturnCR13\r^MCtrl+q Ctrl+m

[see List of ASCII Control Characters]

Newline Convention in Windows, Linux, Mac

Operating System Newline Convention
Operating SystemNewline Convention
Unix, Linux, Mac OS XLF
WindowsCRLF (2 characters)
Mac OS Classic (dated before 2002)CR

Note: Mac OS X prefers LF, but accept the Mac OS Classic's CR too.

Newline at end of file

Many tools or systems/protocols for reading a file, expect a newline at the end of file.

Many editors automatically add a newline at the end of file when you save the file, if it's not already there. (this is true for emacs, but can be turned off.)

This is important to know, because if you manually change the newline character, but after you save the file, a unexpected newline may be quietly added.

Change file line endings between Windows/Unix/Mac

Open the file, then Alt+x set-buffer-file-coding-system. When it prompts you for a coding system, type one of: {mac, dos, unix}. Then, save the file. (on Mac OS X, use “unix”. For Microsoft Windows, use “dos”.)

If you want utf-8 encoding, chose one of:

Which newline convention is used for the current file?

Alt+x describe-variable , then type buffer-file-coding-system.

Change file's line ending in a directory

Emacs: Convert File Line Ending

Why is emacs showing ^M

^M is a carriage return character "/r", unicode codepoint 13.

If emacs displays many ^M in every line, that means there's inconsistent line ending in the file.

Find out what char is ^M ^J ^L

Move cursor to it, then Alt+x describe-char

emacs describe-char CR 2021-12-09
emacs describe-char CR 2021-12-09

How to delete ^M

  1. Goto beginning of the file, then Alt+x query-replace, then insert the Carriage Return char by Ctrl+q Ctrl+m for the find string. For replacement string just press Enter for empty string.
  2. Alt+x set-buffer-file-coding-system, then type one of: {mac, dos, unix}. Then, save the file.

What does Ctrl+q mean?

Ctrl+q is the shortcut for the command quoted-insert. It will let you enter the next charater literally. For example, to type a literal tab, press Ctrl+q then the Tab key.

The Carriage Return has caret notation of “^M”, so, press Ctrl+q Ctrl+m will insert it.

[see Emacs Key Syntax Explained]

Can I change newline from Windows to unix by just deleting ^M

No. When emacs opens a file, it represent all newline by LF, doesn't matter what's the actual newline convention in the file. If emacs displays “^M” (aka CR), that's because the file has inconsistent line endings.

When you save a file, emacs automatically use the correct newline char when writing the buffer to file, according to the value of buffer-file-coding-system. (Alt+x describe-variable to see variable's value.)

Also, emacs may automatically add a newline to the end of the file when you save it. Which character it adds depends on the value of buffer-file-coding-system.

So, if you manually add/remove newline char to every line, but you didn't change the buffer's buffer-file-coding-system, then when you save, emacs may add a newline char to the end of the file that is inconsistent to what you expect.

The auto adding newline is controlled by the variables require-final-newline and mode-require-final-newline.

Emacs Buffers Always Use LF

In emacs buffer, the newline is always just Line Feed, regardless what operating system. Emacs will display {^J, ^M} only when the file's newline chars cannot be interpreted in a consistent way.

Thanks to Stefan Monnier [http://www.iro.umontreal.ca/~monnier/ ] for a major tip on this newline issue in emacs.

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Tutorial

Quick Start

Manage Windows

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Editing Brackets

Org Mode

HTML

Emacs Efficiency

Misc