Search code examples
emacskey-bindings

Does the C-m key combination in emacs generate "\r\n" or just "\n"


The emacs docs for C-m key (ie. C-h k C-m) displays the following

newline is an interactive compiled Lisp function in `simple.el'.

(newline &optional ARG)

Insert a newline, and move to left margin of the new line if it's blank.
If `use-hard-newlines' is non-nil, the newline is marked with the
text-property `hard'.
With ARG, insert that many newlines.
Call `auto-fill-function' if the current column number is greater
than the value of `fill-column' and ARG is nil.

But it does not say newline is in the context of dos ("\r\n") or unix ("\n").


Solution

  • Internally, Emacs stores a Unix newline, i.e. "\n", in its buffers, similar to what most C programs do when reading text files. If there is a "\r" in an Emacs buffer, it will be displayed as ^M.

    On the other hand, Emacs remembers the newline convention used for each opened file. If you open a file with "\r\n" line breaks, modify it, and save it, then Emacs will convert all newlines back to "\r\n".