[英]vim: How to replace line breaks within a specific XML node?
如何替換特定XML節點內的換行符?
例如: <content:encoded></content:encoded>
^J
到<br/>
代碼示例:
<content:encoded><![CDATA[
Communities across the Northwest Territories have harvested a bumper crop
of produce ranging from potatoes and carrots to spinach and lettuce thanks to
dedicated community gardeners and the Government of the Northwest Territories’ (GNWT).
]></content:encoded>
如果只有這樣一個范圍,則將執行以下操作:
/<content:encoded>/,/<\/content:encoded>/s#$#<br/>#
這會在(打開/關閉)標簽所限定的范圍內執行:substitute
命令。 該示例添加了<br/>
標記,如果要將所有內容壓縮到一行中,請搜索\\n
而不是$
( \\n\\s*
也可以刪除縮進)。
如果要替換多個這樣的標簽,請在命令前加上:global
。 這將對緩沖區中找到的所有標簽執行替換(這次是從包含開始標簽的當前行到下一個結束標簽)(您可以通過添加一個不同的范圍來再次影響,例如1,100global
)。
您可以使用宏:
qq
開始錄音 /content:encoded<cr>
轉到下一個標記。 注意<cr>
表示按回車鍵。 vit
在標簽中直觀地選擇 :'<,'>s/\\n\\s*/<br\\/>/g
代替所有換行符后跟任意數量的用空格<br/>
。 注意'<,'>
將由vim自動添加。 q
停止錄制 5@q
重復5次。 也可能使用正則表達式,但是我的正則表達式foo並沒有做到這一點。
使用適當的XML處理工具。 例如, xsh :
open file.xml ;
for my $text in //content:encoded/text() {
my $lines = xsh:split("\n", $text) ;
for $lines {
my $t := insert text (.) before $text ;
insert element br before $text ;
}
delete $text;
}
save :b ;
我認為解決方案是直觀地選擇行,然后:s#\\n#<br/>\\r#g
它將行尾替換為<br/>
。
如果要在每個內部標簽中替換它,則應使用:: :g /<content:encoded>/;/\\/content:encoded/ s#\\n#<br/>\\r#g
/// :g /<content:encoded>/;/\\/content:encoded/ s#\\n#<br/>\\r#g
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.