[ACCEPTED]-Hiding ^M in emacs-emacs
(defun remove-dos-eol ()
"Do not show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
Solution by Johan Bockgård. I found it here.
0
Modern versions of emacs know how to handle 7 both UNIX and DOS line endings, so when 6 ^M shows up in the file, it means that there's 5 a mixture of both in the file. When there 4 is such a mixture, emacs defaults to UNIX 3 mode, so the ^Ms are visible. The real fix 2 is to fix the program creating the file 1 so that it uses consistent line-endings.
What about?
C-x RET c dos RET C-x C-f FILENAME RET
I made a file that has two lines, with 4 the second having a carriage return. Emacs 3 would open the file in Unix coding, and 2 switching coding system does nothing. However, the 1 universal-coding-system-argument
above works.
I believe you can change the line coding 4 system the file is using to the Unix format 3 with
C-x RET f UNIX RET
If you do that, the mode line should 2 change to add the word "(Unix)", and all 1 those ^M's should go away.
If you'd like to view the log files and 6 simply hide the ^M's rather than actually 5 replace them you can use Drew Adam's highlight extension to 4 do so.
You can either write elisp code or 3 make a keyboard macro to do the following
select the whole buffer
hlt-highlight-regexp-region
C-q C-M
hlt-hide-default-face
This 2 will first highlight the ^M's and then hide 1 them. If you want them back use `hlt-show-default-face'
Edric's answer should get more attention. Johan 10 Bockgård's solution does address the poster's 9 complaint, insofar as it makes the ^M's 8 invisible, but that just masks the underlying 7 problem, and encourages further mixing of 6 Unix and DOS line-endings.
The proper solution 5 would be to do a global M-x replace-regexp
to turn all line endings 4 to DOS ones (or Unix, as the case may be). Then 3 close and reopen the file (not sure if M-x revert-buffer
would 2 be enough) and the ^M's will either all 1 be invisible, or all be gone.
You can change the display-table entry of 6 the Control-M (^M
) character, to make it displayable 5 as whitespace or even disappear totally 4 (vacuous). See the code in library pp-c-l.el
(Pretty Control-L) for 3 inspiration. It displays ^L
chars in an arbitrary 2 way.
Edited: Oops, I just noticed that @binOr 1 already mentioned this method.
Put this in your .emacs:
(defun dos2unix ()
"Replace DOS eolns CR LF with Unix eolns CR"
(interactive)
(goto-char (point-min))
(while (search-forward "\r" nil t) (replace-match "")))
Now you can simply 1 call dos2unix
and remove all the ^M
characters.
If you encounter ^M
s in received mail in Gnus, you 1 can use W c
(wash CRs), or
(setq gnus-treat-strip-cr t)
sudeepdino008's answer did not work for 3 me (I could not comment on his answer, so 2 I had to add my own answer.).
I was able 1 to fix it using this code:
(defun dos2unix ()
"Replace DOS eolns CR LF with Unix eolns CR"
(interactive)
(goto-char (point-min))
(while (search-forward (string ?\C-m) nil t) (replace-match "")))
Like binOr said add this to your %APPDATA%.emacs.d\init.el 1 on windows or where ever is your config.
;; Windows EOL
(defun hide-dos-eol ()
"Hide ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
(defun show-dos-eol ()
"Show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M ?\^M))
(add-hook 'text-mode-hook 'hide-dos-eol)
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.