所以我正在改变代码
foo() {
至
foo() {
我注意到search模式要求我search\n
,但是当我试图用\n
replace它时,我得到了^@
字符,而我不得不用\r
replace。
对我来说,我觉得很奇怪,我用\n
search并用\r
replace,为什么会这样?
作为参考,我的解决scheme是:%s/\n\s*{/ {\r/g
搜索部分和替换部分的语法是任意的。 一些相同的代码被重新用来表示不同的东西。 是的,这很混乱。
| How to type | In search, means: | In replacement, means: ---------------------------------------------------------------------------- \n | \n | End-of-line | <Nul> 0x0 ^@ | CTRL-V CTRL-J | <Nul> 0x0 | <Nul> 0x0 \r | \r | Carriage return 0xD | "Break the line here" ^M | CTRL-Enter | Carriage return 0xD | "Break the line here" \^M | \ CTRL-V CTRL-ENTER | \ + carriage return 0xD | Carriage return 0xD
当搜索,根据您的平台, 0xD
可能被隐藏,被认为是“换行”的一部分,所以是的…你总是可以返回到你的文件的理智,并强制所有的回车显示打开一个文件,并做:
:e ++ff=unix
同样,替换时,“在这里断开”根据你的平台做不同的事情。 它可能会插入0xA
或0xD 0xA
等
如果这还不够全面:
Technical detail: *NL-used-for-Nul* <Nul> characters in the file are stored as <NL> in memory. In the display they are shown as "^@". The translation is done when reading and writing files. To match a <Nul> with a search pattern you can just enter CTRL-@ or "CTRL-V 000". This is probably just what you expect. Internally the character is replaced with a <NL> in the search pattern. What is unusual is that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul> in the file. {Vi cannot handle <Nul> characters in the file at all} *CR-used-for-NL* When 'fileformat' is "mac", <NL> characters in the file are stored as <CR> characters internally. In the text they are shown as "^J". Otherwise this works similar to the usage of <NL> for a <Nul>. When working with expression evaluation, a <NL> character in the pattern matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL> doesn't work there, it only works to match text in the buffer.
除非处理字面的0x0
或0xD
字符,否则就像你可能已经想到的那样,总是坚持使用\n
进行搜索和\r
进行替换。
也可以看看:
:h /\n :h /\r :hs/\n :hs/\r :hs/\<CR>
我认为你的问题是由于像Unix这样的类Unix操作系统使用换行符(又名“linefeed”)(0x0A)作为行标记的结尾,但是Windows使用了回车符+换行符(0x0D 0x0A)。 vim正在尝试做一些映射,使Windows的两个字节的行结束看起来像一个单一的“行结束”。
在相关的说明中,下面的命令似乎将Windows风格的文件转换为Windows风格的文件,并在Unix机器上将Windows风格的文件转换为Unix风格的文件:
:%s/^V^M/^V^M/g
其中^V
表示按住控制键并按下V,同样^M
表示按住控制键并按下M.