[ACCEPTED]-In emacs, how to strip CR (^M) and leave LF (^J) characters?-hex-editors
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
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
.
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).
There's also a command-line tool called 2 unix2dos/dos2unix that exists specifically to convert line 1 endings.
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.
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.
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.
(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.
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
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 ^M
s in anymode.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.