all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Xah <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: ^M characters
Date: Wed, 22 Oct 2008 14:25:02 -0700 (PDT)	[thread overview]
Message-ID: <6f700367-8aa9-4112-bdbb-59ea98f809b5@f37g2000pri.googlegroups.com> (raw)
In-Reply-To: mailman.1834.1224707393.25473.help-gnu-emacs@gnu.org

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/

       reply	other threads:[~2008-10-22 21:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1834.1224707393.25473.help-gnu-emacs@gnu.org>
2008-10-22 21:25 ` Xah [this message]
2008-10-22 23:32   ` ^M characters Mike Treseler
2008-10-22 23:34     ` ^M characters - typo Mike Treseler
2008-10-23 19:55     ` ^M characters Mike Treseler
2008-10-23  9:49 roodwriter
  -- strict thread matches above, loose matches on Subject: below --
2008-10-22 20:29 Corey Foote
2008-10-22 20:43 ` Parker, Matthew

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6f700367-8aa9-4112-bdbb-59ea98f809b5@f37g2000pri.googlegroups.com \
    --to=xahlee@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.