如何用 vim 中的字节 \x20 替换字节 \xe3\x80\x80?

How to replace bytes \xe3\x80\x80 with byte \x20 in vim?

让我们创建要操作的目标文件。

python3
>>> mfile = open("f:/test.txt","wb")
>>> mfile.write(b'\xe3\x80\x80')
3
>>> mfile.close()

现在用xxd打开f:/test.txt,你会看到里面有三个字节\xe3\x80\x80,我们用utf-8编码的目标文件包含三个字节\xe3\x80\x80.

python3
b'\xe3\x80\x80'.decode('utf-8')
'\u3000'

表示用utf-8编码的test.txt三个字节的unicode是3000.

:s/\%u3000/ /g

s/\%u3000/ /g 可以用 vim.

中的字节 \x20 替换字节 \xe3\x80\x80

问题仍然存在。

:s/\%u3000/\%u20/g 
:s/\%u3000/\%x20/g
:s/\%u3000/\x20/g

这里上面的三种格式都不行,为什么\xe3\x80\x80在vim,</code>中可以用<code>\%u3000表示(白blank) 不能用 \%u20\%x20\x20 表示?

</code>可以表示<code>\x20,白色空白是可打印字符,另外,我想把三个字节\xe3\x80\x80换成latin-1的nbsp?

latin-1编码中的nbsp表示Non-breaking space即NON PRINTABLE CHARACTERS,vim中的表达式如何写?

:s/\%u3000/\%ua0/g 
:s/\%u3000/\%xa0/g
:s/\%u3000/\xa0/g

None 个可以处理这个案例。

您可以输入 \xe3\x80\x80u3000 字符,方法是按 ctrl+v 然后 u 然后是 4 个 Unicode 字符,在你的例子中是 3000 (检查 :help i_CTRL-V_digit ),因为它是一个黑色字符,你只会看到一个 space,你可以输入 :set list 来查看你有那个角色的所有地方,或者在任何情况下将它添加到你的 .vimrc

set listchars=tab:▸\ ,eol:¬,trail:·,extends:#,nbsp:.

现在以同样的方式输入字符,你可以尝试在命令行中替换它,但在这种情况下可以输入 ctrl+v 你可以尝试使用 command-line window (:help cedit).

进入命令模式,在:按下ctrl+f后会打开command-line window 您可以在其中进入插入模式并键入:%s/ctrl+v u3000/ /g 完成后按回车键应用命令。

进入command-line之前先试一下window,因为在使用ctrl+v的时候可能有效,不像使用 ctrl+k (http://vim.wikia.com/wiki/Entering_special_characters)

在图片中没有替换为白色 space / /,而是替换为 ---- 只是为了直观地看到变化。

1.How 在 vim?

中编辑文件时输入不可打印的字符

在插入模式下:

1.ctrl+v (ctrl+q if ctrl+v call paste from regitor)
2.input u 
3.input the unicode value of non printable characters
4.input enter key

2.How 在 vim 的 ex 模式的替代命令中输入不可打印的字符?

例如,要将所有字节\xe3\x80\x80替换为\xa0,所有字节的编码都是utf-8。

1. get the byte's unicode value 

`\xe3\x80\x80`'s unicode value is `3000`,   
`\xa0`'s unicode value is `a0`.

2.press `:` into ex mode.
3.:s/\%u3000/    
4:ctrl+v ua0  
do not input enter as above process
5.go on to input `/g`.
6.press enter.