[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ^M characters
From: |
Xah |
Subject: |
Re: ^M characters |
Date: |
Wed, 22 Oct 2008 14:25:02 -0700 (PDT) |
User-agent: |
G2/1.0 |
On Oct 22, 1:29 pm, Corey Foote <coreyfo...@hotmail.com> wrote:
> I copied some text into an email buffer, but
> each line ends with a funny colored character that looks like ^M. What are
> these characters? How can I remove them without having to manually delete each
> one? And how can I insert one myself?
^M is a standard notation for the ascii 13 char named Carriage Return.
Today, this notation has fallen out of use, unfamiliar to probably 99%
of professional programers.
For detail, see:
The Confusion of Emacs's Keystroke Representation
http://xahlee.org/emacs/keystroke_rep.html
When you paste some code involving different EOL char, most editor
deal with this by simply converting them to your current EOL. Usually
they have a preference setting to indicate whether you want this to
happen automatically or literal. I think emacs should also adapt this
behavior.
To replace these unprintable chars, you can use any of the emacs's
find/replace command (e.g. “query-replace”), and type Ctrl+q when you
want to input unprintable or un-typable chars. Alternatively, you can
change the buffer's file encoding. See:
Q: How to change file line endings between Mac/Dos/Unix?
A: Open the file, then do “Alt+x set-buffer-file-coding-system” (Ctrl-
x RET f). Give it a value of mac, dos, unix. Then, when you save the
file, it'll be saved with the proper encoding for newlines.
Note: Unixes (including Linuxes and Mac OS X) uses LF (ascii 10; line
feed) for newline. Mac OS Classic uses CR (ascii 13; carriage return)
for newline. (Mac OS X prefers LF but accepts CR too) Windows uses CR
followed by LF ("\r\n") for its newline char. See wikipedia newline↗
for detail.
To do it batch on a list of files, use the following lisp code:
(defun to-unix-eol (fpath)
"Change file's line ending to unix convention."
(let (mybuffer)
(setq mybuffer (find-file fpath))
(set-buffer-file-coding-system 'unix) ; or 'mac or 'dos
(save-buffer)
(kill-buffer mybuffer)
)
)
(mapc 'to-unix-eol
(list
"~/jane/myfile1"
"~/jane/myfile2"
"~/jane/myfile3"
; ...
)
)
To use the code, first edit the list of files above. Then, select all
the code, type “Alt+x eval-region”. That's it.
If you want the function to work on marked files in dired, then use
the following code:
(defun dired-2unix-marked-files ()
"Change to unix line ending for marked (or next arg) files."
(interactive)
(mapc 'to-unix-eol (dired-get-marked-files))
)
Select the code and do “Alt+x eval-region”, then “Alt+x dired”, then
press “m” to mark the files you want, then do “Alt+x dired-dos2unix-
marked-files”.
Xah
∑ http://xahlee.org/
☄