Hiding ^M in emacs

后端 未结 12 817
陌清茗
陌清茗 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:24

    What about?

    C-x RET c dos RET C-x C-f FILENAME RET
    

    I made a file that has two lines, with the second having a carriage return. Emacs would open the file in Unix coding, and switching coding system does nothing. However, the universal-coding-system-argument above works.

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

    Modern versions of emacs know how to handle both UNIX and DOS line endings, so when ^M shows up in the file, it means that there's a mixture of both in the file. When there is such a mixture, emacs defaults to UNIX mode, so the ^Ms are visible. The real fix is to fix the program creating the file so that it uses consistent line-endings.

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

    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 call dos2unix and remove all the ^M characters.

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

    Like binOr said add this to your %APPDATA%.emacs.d\init.el 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)
    
    0 讨论(0)
  • 2020-12-12 13:29

    I believe you can change the line coding system the file is using to the Unix format with

    C-x RET f UNIX RET
    

    If you do that, the mode line should change to add the word "(Unix)", and all those ^M's should go away.

    0 讨论(0)
  • 2020-12-12 13:32
    (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 讨论(0)
提交回复
热议问题