Emacs: Newline Representations ^M ^J ^L

Advertise Here

, ,

This page explains line ending conventions Windows, Unix, Mac, and how to change them with emacs.

Here's a short table of commonly used unprintable ASCII characters.

NameAbbrevASCII CodeString NotationCaret NotationInput method
Horizontal TabHT9\t^ICtrl+q Ctrl+i
Line FeedLF10\n^JCtrl+q Ctrl+j
Carriage ReturnCR13\r^MCtrl+q Ctrl+m

Following is the newline convention in different operating systems.

Operating SystemNewline ConventionNotes
Unix, Linux, Mac OS X\nMac OS X prefers \n, but accept the Mac OS Classic's \r too.
Windows\r\n
Mac OS Classic\r

Why does emacs show ^M in a buffer?

If emacs shows that, it's probably because you have mixed characters of ^M and ^J and emacs cannot interpret them consistently as newlines.

To fix it, call set-buffer-file-coding-system, then give one of: {mac, dos, unix}. Then, save the file.

If that does not fix it, you can use find & replace to remove it manually.

How to delete ^M manually?

Call query-replaceAlt+%】, then type the Carriage Return char by 【Ctrl+q Ctrl+m】 for the find string. For replacement string just press Enter for empty string.

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. Tab is “^I”, so 【Ctrl+q Ctrl+i】 inserts a tab. Same for “^J”

For detail about unprintable ASCII chars, their notations, input methods, notation for input methods, see: Emacs's Key Notations Explained (/r, ^M, C-m, RET, <return>, M-, meta).

Can i change newline convention from Windows to unix by just deleting ^M?

Not really. When emacs opens a file, it represent all newline by “^j” (Line Feed; ASCII 10; unix convention), doesn't matter what's the actual newline convention in the file. If emacs displays “^M” (ASCII 13; carriage return), 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”.

Also, emacs may automatically add a newline to the end of the file when you save it. Which character it adds depends on the current file encoding system. So, if you manually change the newline char, emacs may add one that is inconsistent to what you expect. The auto adding newline is controlled by the variables “require-final-newline” and “mode-require-final-newline”.

If you want to convert line ending from different OS, always first call set-buffer-file-coding-system, with a value of {mac, dos, unix}. (on Mac OS X, use “unix”.)

How to know which newline convention is used by emacs for the current file?

Call describe-variableF1 v】, then “buffer-file-coding-system”.

How to quickly find out what ASCII char are those ^M ^J ^L?

Place your cursor on it, then call describe-char.

How to change file line endings between Windows/Unix/Mac?

Open the file, then call set-buffer-file-coding-systemCtrl+x Enter f】. When it prompts you for a coding system, type one of: {mac, dos, unix}, when you save the file, it'll be saved with the proper encoding for newlines.

To do it batch on a list of files, use the following lisp code:

(defun to-unix-eol (fPath)
  "Change file's line ending to unix convention."
  (let (myBuffer)
    (setq myBuffer (find-file fPath))
    (set-buffer-file-coding-system 'unix) ; or 'mac or 'dos
    (save-buffer)
    (kill-buffer myBuffer)
   )
)

(mapc 'to-unix-eol
 (list
"~/jane/myfile1"
"~/jane/myfile2"
"~/jane/myfile3"
;   )
)

To use the code, first edit the list of files above. Then, select all the code, then call eval-region. That's it.

If you want the function to work on marked files in dired, then use the following code:

(defun to-unix-eol-on-marked-files ()
  "Change to unix line ending for marked (or next arg) files."
  (interactive)
  (mapc 'to-unix-eol (dired-get-marked-files))
)

Select the code, then call eval-region, then call dired, then press m to mark the files you want, then call “to-unix-eol-on-marked-files”.

Emacs Buffers Always Use LF

In emacs buffer, the newline char is always just Line Feed (^J; ASCII 10), regardless what OS you are running emacs on. Emacs will display {^J, ^M} only when the file's newline chars cannot be interpreted in a consistent way.

Thanks to Stefan Monnier for a major tip on this newline issue in emacs.

blog comments powered by Disqus