Question

I'm writing a config file and I need to define if the process expects a windows format file or a unix format file. I've got a copy of the expected file - is there a way I can check if it uses \n or \r\n without exiting emacs?

Was it helpful?

Solution

If it says (DOS) on the modeline when you open the file on Unix, the line endings are Windows-style. If it says (Unix) when you open the file on Windows, the line endings are Unix-style.

From the Emacs 22.2 manual (Node: Mode Line):

If the buffer's file uses carriage-return linefeed, the colon changes to either a backslash ('\') or '(DOS)', depending on the operating system. If the file uses just carriage-return, the colon indicator changes to either a forward slash ('/') or '(Mac)'. On some systems, Emacs displays '(Unix)' instead of the colon for files that use newline as the line separator.

Here's a function that – I think – shows how to check from elisp what Emacs has determined to be the type of line endings. If it looks inordinately complicated, perhaps it is.

(defun describe-eol ()
  (interactive)
  (let ((eol-type (coding-system-eol-type buffer-file-coding-system)))
    (when (vectorp eol-type)
      (setq eol-type (coding-system-eol-type (aref eol-type 0))))
    (message "Line endings are of type: %s"
             (case eol-type
               (0 "Unix") (1 "DOS") (2 "Mac") (t "Unknown")))))

OTHER TIPS

If you go in hexl-mode (M-x hexl-mode), you shoul see the line termination bytes.

Open the file in emacs using find-file-literally. If lines have ^M symbols at the end, it expects a windows format text file.

The following Elisp function will return nil if no "\r\n" terminators appear in a file (otherwise it returns the point of the first occurrence). You can put it in your .emacs and call it with M-x check-eol.

(defun check-eol (FILE)
  (interactive "fFile: ")
  (set-buffer (generate-new-buffer "*check-eol*"))
  (insert-file-contents-literally FILE)
  (let ((point (search-forward "\r\n")))
    (kill-buffer nil)
    point))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top