如何去掉 CR (^M) 并保留 LF (^J) 字符?
我正在尝试使用 Hexl 模式手动从文本文件中删除一些特殊字符,但不知道如何在 Hexl 模式下删除任何内容。
我真正想要的是删除回车符并保留换行符。
Hexl 模式是执行此操作的正确方法吗?
I am trying to use Hexl mode to manually remove some special characters from a text file and don't see how to delete anything in Hexl mode.
What I really want is to remove carriage return and keep linefeed characters.
Is Hexl mode the right way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
无需寻找替代。 就用吧。
您还可以通过设置文件编码
No need to find replace. Just use.
You can also set the file encoding through
哎呀。 ^J^M 需要作为两个文字字符输入。
使用
cq
cj
、cq
cm
并使用cq
<代码>cj。Oops. That ^J^M needs to be entered as two literal characters.
Use
c-q
c-j
,c-q
c-m
and for the replacement string, usec-q
c-j
.为此不需要 hexl 模式。 只需对 ^J^M 进行全局搜索并替换为 ^J 对我有用。 :) 然后保存文件,终止缓冲区,然后重新访问该文件,以便窗口显示新的文件模式(Unix 与 DOS)。
No need for hexl-mode for this. Just do a global-search-and-replace of ^J^M with ^J Works for me. :) Then save the file, kill the buffer, and revisit the file so the window shows the new file mode (Unix vs DOS).
还有一个名为 unix2dos/dos2unix 的命令行工具,专门用于转换行结尾。
There's also a command-line tool called unix2dos/dos2unix that exists specifically to convert line endings.
假设您希望将 DOS 编码文件更改为 UNIX 编码,请使用 Mx set-buffer-file-coding-system (Cx RET f) 将编码系统设置为“unix”并保存文件。
Assuming you want a DOS encoded file to be changed into UNIX encoding, use M-x set-buffer-file-coding-system (C-x RET f) to set the coding-system to "unix" and save the file.
如果要删除回车符(通常显示为
^M
)并保留换行符。 您可以直接访问该文件而不进行任何转换:因为带有回车符的文件通常在 DOS 模式下显示(隐藏回车符)。 模式行可能会在左侧显示
(DOS)
。完成此操作后,
^M
将显示,您可以像删除任何字符一样删除它们。If you want to remove a carriage return (usually displayed as
^M
) and leave the line feed. You can just visit the file w/out any conversion:Because a file with carriage returns is generally displayed in DOS mode (hiding the carriage returns). The mode line will likely display
(DOS)
on the left side.Once you've done that, the
^M
will show up and you can delete them like you would any character.您不需要使用十六进制模式。 相反:
要点是,即使您不知道如何输入要替换的内容,您也可以随时选择/复制它。
You don't need to use hexl-mode. Instead:
The point is that even if you don't how to enter what you are trying to replace, you can always select/copy it.
(在十六进制模式下)我不确定您是否可以删除字符。 我总是将它们转换为空格或其他字符,切换到常规文本编辑器,然后在那里删除它们。
(in hexl mode) I'm not sure that you can delete characters. I've always converted them to spaces or some other character, switched to the regular text editor, and deleted them there.
我使用这个功能:
I use this function:
来自 http://www.xsteve.at/prg/emacs/xsteve-functions .el:
将其添加到您的
.emacs
中,并通过Mx xsteve-remove-control-M
运行它或将其绑定到更简单的键。 它会在任何模式下去除^M
。From http://www.xsteve.at/prg/emacs/xsteve-functions.el:
Add this to your
.emacs
and run it viaM-x xsteve-remove-control-M
or bind it to a easier key. It will strip the^M
s in anymode.