2013-09-23 189 views
-1

我有許多行的文本文件。如何有條件地移動排隊?

test = 
more text more text more text more text 
more text more text more text more text 
... etc.... 
more text more text more text more text 
more text more text more text more text 
1 text 
test2 = 
more text more text more text more text 
more text more text more text more text 
3 more text 

我想要做的是移動線'開始的數 並附他們的第一行後發現(倒退),與「= \ s」的

結束預計產出:

test = 1 text 
more text more text more text more text 
more text more text more text more text 
... etc.... 
more text more text more text more text 
more text more text more text more text 
test2 = 3 more text 
more text more text more text more text 
more text more text more text more text 

我不知道該怎麼做。
有人可以幫助我嗎?

回答

4

使用:global:norm:move並有可能使用搜索作爲目標Ex命令:

:g/^\d/m?.*=$|norm kJ 

擊穿:

:g/pattern/command " executes command for every line matching pattern 
^\d    " pattern for "lines that start with a number" 
m?.*=$    " move matched line to right below the first 
        " line ending with = upward 
|     " separator between Ex commands 
norm    " execute normal mode command 
kJ     " go to line above and join 
+0

與我的宏觀相同的想法。但是:g命令會比宏命令好。 +1 – Kent

+3

Vim golf:':g/^ \ d/m?。* = $ | -j' –

+0

非常感謝romainl! – Reman

2

宏可以幫助...

/^\d<cr>:.m?=<cr>kJ 

簡短說明:

/^\d " find line beginning with number 
:.m?= " move current line under the previous line with (=) 
kJ  "move cursor back to the line with (=), and join the next 

,它正在像:

(看來我輸入一個更?最後n在截圖中,但我不會再記錄它。)

enter image description here

+0

太感謝您在肯特!非常好的解決方案,但正如你所提到的,也許Romainl的解決方案更容易。 – Reman