Emacs: Convert File Line Ending 📜
Here's emacs command that convert file line endings (Linux, Microsoft Windows, Mac OS 9), on current file, or marked files in dired in batch.
put this in your Emacs Init File:
(defun xah-change-file-line-ending-style (zfpaths z-coding-system) "Change current file or `dired' marked file's newline convention. When called in lisp, z-coding-system is one of 'unix 'dos 'mac or any of accepted emacs coding system. See `list-coding-systems'. URL `http://xahlee.info/emacs/emacs/elisp_convert_line_ending.html' Created: 2016-10-16 Version: 2025-09-26" (interactive (list (if (eq major-mode 'dired-mode) (dired-get-marked-files) (list buffer-file-name)) (let ((completion-ignore-case t) (xmenu '(("Unix Linux MacOSX" . unix) ("Windows" . dos) ("MacOS9" . mac))) xstyle) (setq xstyle (completing-read "Line ending:" xmenu nil t nil nil (caar xmenu))) (cdr (assoc xstyle xmenu))))) (mapc (lambda (x) (xah-convert-file-coding-system x z-coding-system)) zfpaths))
(defun xah-convert-file-coding-system (zfpath z-coding-system) "Convert file's encoding. zfpath is full path to file. z-coding-system is one of 'unix 'dos 'mac or any of accepted emacs coding system. See `list-coding-systems'. If the file is already opened, it is saved after this command. URL `http://xahlee.info/emacs/emacs/elisp_convert_line_ending.html' Created: 2015-07-24 Version: 2025-09-26" (let ((xbuf-opened-p (get-file-buffer zfpath))) (if xbuf-opened-p (with-current-buffer xbuf-opened-p (if (string-equal z-coding-system buffer-file-coding-system) nil (progn (set-buffer-file-coding-system z-coding-system) (save-buffer)))) (with-temp-file zfpath (insert-file-contents zfpath) (if (string-equal z-coding-system buffer-file-coding-system) nil (set-buffer-file-coding-system z-coding-system))))))