在 Emacs 中隐藏 ^M
有时我需要读取行结尾带有 ^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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
约翰·博克加德 (Johan Bockgård) 的解决方案。 我找到了它 此处。
Solution by Johan Bockgård. I found it here.
现代版本的 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.
关于什么?
我制作了一个有两行的文件,第二行有一个回车符。 Emacs 会以 Unix 编码打开文件,切换编码系统不会执行任何操作。 然而,上面的
universal-coding-system-argument
是有效的。What about?
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.我相信你可以将文件使用的行编码系统更改为 Unix 格式
如果你这样做,模式行应该更改为添加单词“(Unix)”,并且所有这些 ^M 都应该消失。
I believe you can change the line coding system the file is using to the Unix format with
If you do that, the mode line should change to add the word "(Unix)", and all those ^M's should go away.
如果您想查看日志文件并简单地隐藏 ^M 而不是实际替换它们,您可以使用 Drew Adam 的
您可以编写 elisp 代码或创建键盘宏来执行以下操作。
这将首先突出显示 ^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
This will first highlight the ^M's and then hide them. If you want them back use `hlt-show-default-face'
艾德里克的回答应该得到更多关注。 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 ifM-x revert-buffer
would be enough) and the ^M's will either all be invisible, or all be gone.您可以更改 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 librarypp-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.
将其放入您的 .emacs 中:
现在您只需调用
dos2unix
并删除所有^M
字符即可。Put this in your .emacs:
Now you can simply call
dos2unix
and remove all the^M
characters.如果您在 Gnus 中收到的邮件中遇到
^M
,您可以使用W c
(清洗 CR),或者If you encounter
^M
s in received mail in Gnus, you can useW c
(wash CRs), or使用 dos2unix、unix2dos(现在 tfrodos)怎么样?
what about using dos2unix, unix2dos (now tofrodos)?
sudeepdino008的答案对我不起作用(我无法评论他的答案,所以我不得不添加我自己的答案。)。
我能够使用以下代码修复它:
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:
就像 binOr 所说的,将其添加到 Windows 上的 %APPDATA%.emacs.d\init.el 或您的配置中。
Like binOr said add this to your %APPDATA%.emacs.d\init.el on windows or where ever is your config.