在 Emacs 中隐藏 ^M

发布于 2024-07-16 09:29:27 字数 242 浏览 9 评论 0原文

有时我需要读取行结尾带有 ^M (control-M) 的日志文件。 我可以执行全局替换来删除它们,但随后会在日志文件中记录更多内容,当然,它们都会回来。

设置 Unix 风格或 dos 风格的行尾编码似乎没有太大区别(但 Unix 风格是我的默认设置)。 我正在使用未决定的(unix | dos)编码系统。

我在 Windows 上阅读 log4net 创建的日志文件(尽管 log4net 显然不是这种烦恼的唯一来源)。

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 file and, of course, they all come back.

Setting Unix-style or dos-style end-of-line encoding doesn't seem to make much difference (but Unix-style is my default). I'm using the undecided-(unix|dos) coding system.

I'm on Windows, reading log files created by log4net (although log4net obviously isn't the only source of this annoyance).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

财迷小姐 2024-07-23 09:29:27
(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 []))

约翰·博克加德 (Johan Bockgård) 的解决方案。 我找到了它 此处

(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.

尐偏执 2024-07-23 09:29:27

现代版本的 emacs 知道如何处理 UNIX 和 DOS 行结尾,因此当文件中出现 ^M 时,意味着文件中混合了两者。 当存在这种混合时,emacs 默认为 UNIX 模式,因此 ^M 是可见的。 真正的解决方法是修复创建文件的程序,以便它使用一致的行结尾。

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.

韶华倾负 2024-07-23 09:29:27

关于什么?

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

我制作了一个有两行的文件,第二行有一个回车符。 Emacs 会以 Unix 编码打开文件,切换编码系统不会执行任何操作。 然而,上面的universal-coding-system-argument是有效的。

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.

萌酱 2024-07-23 09:29:27

我相信你可以将文件使用的行编码系统更改为 Unix 格式

C-x RET f UNIX RET

如果你这样做,模式行应该更改为添加单词“(Unix)”,并且所有这些 ^M 都应该消失。

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.

梦旅人picnic 2024-07-23 09:29:27

如果您想查看日志文件并简单地隐藏 ^M 而不是实际替换它们,您可以使用 Drew Adam 的

您可以编写 elisp 代码或创建键盘宏来执行以下操作。

select the whole buffer
hlt-highlight-regexp-region
C-q C-M
hlt-hide-default-face

这将首先突出显示 ^M,然后隐藏它们。 如果你想让它们回来,请使用“hlt-show-default-face”

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'

八巷 2024-07-23 09:29:27

艾德里克的回答应该得到更多关注。 Johan Bockgård 的解决方案确实解决了发帖者的抱怨,因为它使 ^M 不可见,但这只是掩盖了根本问题,并鼓励进一步混合 Unix 和 DOS 行尾。

正确的解决方案是执行全局 Mx Replace-regexp所有行结尾转换为 DOS 行结尾(或 Unix,视情况而定)。 然后关闭并重新打开文件(不确定 Mx revert-buffer 是否足够),^M 将全部不可见,或者全部消失。

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.

自控 2024-07-23 09:29:27

您可以更改 Control-M (^M) 字符的显示表条目,使其可显示为空白,甚至完全消失(空)。 请参阅库中的代码 pp-cl.el (Pretty Control-L) 获取灵感。 它以任意方式显示 ^L 字符。

编辑:哎呀,我刚刚注意到@binOr 已经提到了这个方法。

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.

谎言 2024-07-23 09:29:27

将其放入您的 .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 "")))

现在您只需调用 dos2unix 并删除所有 ^M 字符即可。

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.

夜未央樱花落 2024-07-23 09:29:27

如果您在 Gnus 中收到的邮件中遇到 ^M,您可以使用 W c(清洗 CR),或者

(setq gnus-treat-strip-cr t)

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

(setq gnus-treat-strip-cr t)
浅暮の光 2024-07-23 09:29:27

使用 dos2unix、unix2dos(现在 tfrodos)怎么样?

what about using dos2unix, unix2dos (now tofrodos)?

救赎№ 2024-07-23 09:29:27

sudeepdino008的答案对我不起作用(我无法评论他的答案,所以我不得不添加我自己的答案。)。

我能够使用以下代码修复它:

(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 "")))

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 "")))
泪意 2024-07-23 09:29:27

就像 binOr 所说的,将其添加到 Windows 上的 %APPDATA%.emacs.d\init.el 或您的配置中。

;; 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文