2013-04-06 40 views
0

我有一個文件:轉義嵌套的替代品()的^ @字符命令導致7.2

"""I'm a multiline string. 
I say 
    "hey, single line string", 
and it says 
    "\they, multiline string,\nI can do multiple lines\ntoo". 
and I say, 
    "it's cute that you think you can". 

Yeah, I'm kind of a jerk.""" 

我可以使用嵌套substitute()改造它:

:%s/"""\(\_.\{-}\)"""/\='"'.substitute(submatch(1),'["\\\n]','\\\0','g').'"'/g 

在VIM 7.3,我得到了我想要的:

"I'm a multiline string.\ 
I say \ 
    \"hey, single line string\", \ 
and it says \ 
    \"\\they, multiline string,\\nI can do multiple lines\\ntoo\".\ 
and I say, \ 
    \"it's cute that you think you can\".\ 
\ 
Yeah, I'm kind of a jerk." 

然而,在vim 7.2,我得到了相同的輸入和命令不同的結果:

"I'm a multiline string.^@I say ^@ "hey, single line string", ^@and it says ^@ "\they, multiline string,\nI can do multiple lines\ntoo".^@and I say, ^@ "it's cute that you think you can".^@^@Yeah, I'm kind of a jerk." 

(其中^@,據我所知,一個零字節)。

爲什麼我會得到如此不同的行爲?我應該如何修改我的:%s命令才能在7.2和7.3中獲得相同的效果?

+0

我不知道VIM可言,但它可能有一個問題與'\ 0',請嘗試使用別的東西,而不是也許你。可以試試'$ 0'或$ $'' – Qtax 2013-04-06 03:44:55

+0

我沒有7.2的訪問權限,只有7.3和7.0,我可以在7.0中使用以下代碼:':%s /「」「\(\ _。\ { - } \)「」「/ \ = escape(substitute(submatch(1),'['\\\ n]','\\&','g'),'\')/ g'。你可以si mplify你的命令:'%s /""""\(\_.\{-}\)"""/\= escape(submatch(1),「\ n \」\\「)/ g'。 – 2013-04-06 06:00:47

回答

1

我想您所遇到的行爲是由於補丁7.3.225固定的錯誤:在一個替代

「\ n」()裏面「分:秒」未正確處理

Vim 7.2是從2008和非常過時。應該可以安裝最新的7.3版本;如果你不能找到你的發行適當包裝(適用於Windows,從Cream project檢查二進制文件,它也是在Linux上compile(例如,從水銀源)也不是很困難的。

如果你需要支持較老版本的Vim,你會發現一個解決方法,你可以實現一個條件:

if v:version > 703 || v:version == 703 && has('patch225') 
    " new implementation 
else 
    " workaround 
endif