Saturday, November 7, 2009

HOWTO: dos2unix in emacs

Overview
I switch between Linux, OSX, and Windows quiet a bit. One problem with that can be the dreaded EOL issue.

For example, if you add #!/usr/bin/env python and have a DOS/Windows formatted file, running it in UNIX right from a shell may not work.

For my development, I prefer to standardize on UNIX format. For the languages I use, I know it will not cause any issues regardless of where they are run.

Requirements
If you are using linux, OSX, or Solaris you will edit your ~/.emacs file. If you are running Windows, you will be editing your %HOME%/_emacs

For this howto, emacs-22 is being used under Ubuntu Linux 9.10. I doubt the OS or version of emacs will make a difference.

Force Unix EOL
To avoid having to deal with EOL on your own code, add the following to your .emacs file.
; Ensure we stay in UNIX mode for EOL char
(setq inhibit-eol-conversion t)
Now emacs will only use UNIX EOL Plus, you will see ^M in your file for each line with a DOS/Windows EOL.

dos2unix Code
I found this code and have been using it. Why re-invent the wheel.
;;; Convert DOS file format to Unix
;; look at
;; M-x comint-strip-ctrl-m
;;   Command: Strip trailing `^M' characters from the current output group.
;; from: elf@ee.ryerson.ca (Luis Fernandes)
;; 22 May 1997
;;; Usage: M-x dos2unix
;;;
(defun dos2unix ()
"Convert this entire buffer from MS-DOS text file format to UNIX."
(interactive)
(save-excursion
 (goto-char (point-min))
 (replace-regexp "\r$" "" nil)
 (goto-char (1- (point-max)))
 (if (looking-at "\C-z")
     (delete-char 1))))
To use, press ESC-x and type dos2unix After pressing return, you will see the magic happen. I suggest making a backup before doing any global changes to anything. All ^M will disappear.

Final Thoughts
Adding this lisp code will help you standardize on UNIX EOL. You will find the full .emacs file here.

No comments: