如何使用emacs查找和删除文本文件中的不可见字符 [英] How to find and remove the invisible characters in text file using emacs
问题描述
$ file COPYING
COPYING:ASCII英文文本,带有CRLF行终止符
我尝试使用dos2unix将其转换为unix样式。以下是输出
$ dos2unix COPYING
dos2unix:跳过二进制文件COPYING
我很惊讶,发现dos2unix程序将其报告为二进制文件。然后使用其他编辑器(而不是emacs),我发现该文件包含一个控制字符。我有兴趣使用emacs查找文件中的所有隐形字符。
通过Googling,我发现以下解决方案使用了tr
tr -cd'\11\12\40-\176'< file_name
如何在emacs中执行相同的操作。我尝试了hexl模式。 hexl模式在单个缓冲区中显示文本及其对应的ascii值,这是非常好的。如何查找ASCII数值不同于11-12,40-176(即标签,空格和可见字符)的字符。我试图为该搜索创建一个正则表达式,但这是相当复杂的。
默认情况下,Emacs不会隐藏任何字符。按 Ctrl + 元 + %或 Esc 然后 Ctrl + %如果前者对你的手指太硬,或者如果你愿意,或者code> Mx replace-regexp RET 。然后,对于正则表达式,输入
[^ @ - ^ H ^ K - ^ _ ^?]
但是,在我写的 ^ H
中,键入 Ctrl + Q 然后是 Ctrl + H ,以字面的方式输入control-H您可以按 Ctrl + Q 然后 Ctrl + 空格为 ^ @
,通常 Ctrl + Q 然后 Backspace 为 ^?
。将这个正则表达式的所有出现替换为空字符串。
由于您在Emacs中打开文件,因此您可以在此处更改其行尾。按 Cx RET f
( Ctrl + X 返回 F ),然后输入 us-ascii-unix
作为文件的新的所需编码。
I have a txt file named COPYING which is edited on windows. It contains windows style eol
$ file COPYING
COPYING: ASCII English text, with CRLF line terminators
I tried to convert it to unix style using the dos2unix. The below is the output
$ dos2unix COPYING
dos2unix: Skipping binary file COPYING
I was surprised to find that dos2unix program reports it as an binary file. Then using some other editor(not emacs) i found that the file contains a control character. I am interested to find all the invisible characters in the file using emacs.
By Googling i have found the following solution which uses tr
tr -cd '\11\12\40-\176' < file_name
How to do the same in emacs way. I tried the hexl-mode. The hexl-mode shows text and their corresponding ascii values in a single buffer which is great. How to find the characters which has ASCII values other than 11-12, 40-176(i.e tab, space and visible characters). I tried to create a regular expression for that search, but it is quite complicated.
Emacs won't hide any character by default. Press Ctrl+Meta+%, or Esc then Ctrl+% if the former is too hard on your fingers, or M-x replace-regexp RET
if you prefer. Then, for the regular expression, enter
[^@-^H^K-^_^?]
However, where I wrote ^H
, type Ctrl+Q then Ctrl+H, to enter a "control-H" character literally, and similarly for the others. You can press Ctrl+Q then Ctrl+Space for ^@
, and usually Ctrl+Q then Backspace for ^?
. Replace all occurrences of this regular expression by the empty string.
Since you have the file open in Emacs, you can change its line endings while you're at it. Press C-x RET f
(Ctrl+X Return F) and enter us-ascii-unix
as the new desired encoding for the file.
这篇关于如何使用emacs查找和删除文本文件中的不可见字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!