Hiding ^M in emacs

后端 未结 12 818
陌清茗
陌清茗 2020-12-12 12:54

Sometimes I need to read log files that have ^M (control-M) in the line endings. I can do a global replace to get rid of them, but then something more is logged to the log

相关标签:
12条回答
  • 2020-12-12 13:34

    If you'd like to view the log files and simply hide the ^M's rather than actually replace them you can use Drew Adam's highlight extension to do so.

    You can either write elisp code or 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 will first highlight the ^M's and then hide them. If you want them back use `hlt-show-default-face'

    0 讨论(0)
  • 2020-12-12 13:35

    You can change the display-table entry of the Control-M (^M) character, to make it displayable as whitespace or even disappear totally (vacuous). See the code in library pp-c-l.el (Pretty Control-L) for inspiration. It displays ^L chars in an arbitrary way.

    Edited: Oops, I just noticed that @binOr already mentioned this method.

    0 讨论(0)
  • 2020-12-12 13:43

    what about using dos2unix, unix2dos (now tofrodos)?

    0 讨论(0)
  • 2020-12-12 13:47

    If you encounter ^Ms in received mail in Gnus, you can use W c (wash CRs), or

    (setq gnus-treat-strip-cr t)
    
    0 讨论(0)
  • 2020-12-12 13:49

    Edric's answer should get more attention. Johan Bockgård's solution does address the poster's complaint, insofar as it makes the ^M's invisible, but that just masks the underlying problem, and encourages further mixing of Unix and DOS line-endings.

    The proper solution would be to do a global M-x replace-regexp to turn all line endings to DOS ones (or Unix, as the case may be). Then close and reopen the file (not sure if M-x revert-buffer would be enough) and the ^M's will either all be invisible, or all be gone.

    0 讨论(0)
  • 2020-12-12 13:49

    sudeepdino008's answer did not work for me (I could not comment on his answer, so I had to add my own answer.).

    I was able 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 "")))
    
    0 讨论(0)
提交回复
热议问题