[ACCEPTED]-In emacs, how to strip CR (^M) and leave LF (^J) characters?-hex-editors

Accepted answer
Score: 94

No need to find replace. Just use.

M-x delete-trailing-whitespace

You can 1 also set the file encoding through

C-x RET f unix
Score: 33

Oops. That ^J^M needs to be entered as 2 two literal characters. Use c-q c-j, c-q c-m and for 1 the replacement string, use c-q c-j.

Score: 10

No need for hexl-mode for this. Just do 5 a global-search-and-replace of ^J^M with 4 ^J Works for me. :) Then save the file, kill 3 the buffer, and revisit the file so the 2 window shows the new file mode (Unix vs 1 DOS).

Score: 9

There's also a command-line tool called 2 unix2dos/dos2unix that exists specifically to convert line 1 endings.

Score: 8

Assuming you want a DOS encoded file to 3 be changed into UNIX encoding, use M-x set-buffer-file-coding-system 2 (C-x RET f) to set the coding-system to 1 "unix" and save the file.

Score: 6

If you want to remove a carriage return 8 (usually displayed as ^M) and leave the line 7 feed. You can just visit the file w/out 6 any conversion:

M-x find-file-literally /path/to/file

Because a file with carriage 5 returns is generally displayed in DOS mode 4 (hiding the carriage returns). The mode 3 line will likely display (DOS) on the left side.

Once 2 you've done that, the ^M will show up and 1 you can delete them like you would any character.

Score: 4

You don't need to use hexl-mode. Instead:

  • open file in a way that shows you those ^M's. See M-x find-file-literally /path/to/file above. In XEmacs you can also do C-u C-x C-f and select binary encoding.
  • select the string you want replace and copy it using M-w
  • do M-% (query replace) and paste what you want to copy using C-y
  • present Enter when prompted to what replace it with
  • possible press ! now to replace all occurrences

The 3 point is that even if you don't how to enter 2 what you are trying to replace, you can 1 always select/copy it.

Score: 3

(in hexl mode) I'm not sure that you can 3 delete characters. I've always converted them to spaces or 2 some other character, switched to the regular 1 text editor, and deleted them there.

Score: 2

I use this function:

(defun l/cr-sanitise ()
  "Make sure current buffer uses unix-utf8 encoding.
If necessary remove superfluous ^M. Buffer will need to be saved
for changes to be permanent."
  (interactive)
    (set-buffer-file-coding-system 'utf-8-unix)
    (delete-trailing-whitespace)
    (message "Please save buffer to persist encoding changes."))

0

Score: 1

From http://www.xsteve.at/prg/emacs/xsteve-functions.el:

;02.02.2000
(defun xsteve-remove-control-M ()
  "Remove ^M at end of line in the whole buffer."
  (interactive)
  (save-match-data
    (save-excursion
      (let ((remove-count 0))
        (goto-char (point-min))
        (while (re-search-forward (concat (char-to-string 13) "$") (point-max) t)
          (setq remove-count (+ remove-count 1))
          (replace-match "" nil nil))
        (message (format "%d ^M removed from buffer." remove-count))))))

Add this to your .emacs and run it via M-x xsteve-remove-control-M or 2 bind it to a easier key. It will strip 1 the ^Ms in anymode.

More Related questions