EndOfLineTips

This page is for tips about line ending characters.

If you see ^M in your file, you may have opened a file with DOS-style line endings (carriage return + line feed) while Emacs assumes it has Unix-style line endings (line feed only). (The carriage-return character, sometimes abbreviated as CR, is ^M. The line-feed character, sometimes abbreviated as LF, is ^J.)

You can reopen the file with the correct line ending with a command like ‘C-x C-m r dos’.

If you have a mix of lines that end in ^M and lines that don’t, you can use ‘M-x replace-string RET C-q C-m RET’ to remove the ^M’s. ‘C-q’ quotes the key you press right after it, letting you insert a ^M character. Typing ‘C-m’ won’t work – you have to hold down ‘Control’ while hitting ‘q’ followed by ‘m’.

If you prefer seeing the ^M instead of Emacs autodetecting the file as having dos line endings, then set ‘inhibit-eol-conversion’ to non-‘nil’ (see Recognize Coding in the Emacs manual).


To shift linefeeds from one system (Unix, Mac,) to another system (say, DOS,) set the buffer file coding system.

You can see an example from Elisp in SeijiZenitaniDotEmacs;

Here are the specific codes:

See DosToUnix for other methods.


Automatically convert line endings to unix:

(defun no-junk-please-were-unixish ()
  (let ((coding-str (symbol-name buffer-file-coding-system)))
    (when (string-match "-\\(?:dos\\|mac\\)$" coding-str)
      (set-buffer-file-coding-system 'unix))))

(add-hook 'find-file-hooks 'no-junk-please-were-unixish)

CategoryEditing