如何有条件地移动线?

时间:2013-09-23 14:27:23

标签: vim conditional move lines

我有一个包含许多行的文本文件。

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

我不知道该怎么做。
有人能帮助我吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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