查找所有子匹配以重新排序句子

时间:2018-05-18 15:31:11

标签: vim

在Windows 7上使用gvim 8.0,我想重新排序句子模式的一部分:

之前:

Execution time: 0.22s Statement 1 of 8 finished (2018-05-18 06:48:35.231)
Execution time: 0.22s Statement 1 of 8 finished (2018-05-18 06:54:01.259)
Execution time: 0.22s Statement 1 of 8 finished (2018-05-18 07:05:54.480)
<et al>

后:

Statement 1 of 8 finished (2018-05-18 06:48:35.231) Execution time: 0.22s 
Statement 1 of 8 finished (2018-05-18 06:54:01.259) Execution time: 0.22s
Statement 1 of 8 finished (2018-05-18 07:05:54.480) Execution time: 0.22s 
<et al>

我以为我会捕获子匹配,​​然后替换;我似乎无法获得多个子匹配 - 我尝试过:

/\v^([^S]*)

并且匹配了我想要的第一个子匹配,但随后:

/\v^([^S]*),([^S]*)

似乎找不到任何东西。

这是正确的做法吗?我做错了什么?

3 个答案:

答案 0 :(得分:3)

对于您的输入句子,您可以使用替换:

 %s/\v(.*) (Statement.*)/\2 \1/g

这将捕获Statement之前的文本并将其放在句子的末尾。输出:

Statement 1 of 8 finished (2018-05-18 06:48:35.231) Execution time: 0.22s
Statement 1 of 8 finished (2018-05-18 06:54:01.259) Execution time: 0.22s
Statement 1 of 8 finished (2018-05-18 07:05:54.480) Execution time: 0.22s

答案 1 :(得分:1)

你的文字中没有逗号,为什么你的搜索模式中有一个逗号?

我发现awk更直观:

:%!awk '{print $4, $5, $6, $7, $8, $9, $10, $1, $2, $3}'

答案 2 :(得分:0)

:g/^E/exec "norm! dtSA \<esc>p"

g ........... globally
^E .......... at the lines that start with "E"
exec ........ execute
normal! ..... in normal mode
dtS ......... delete [un]till before next "S"
A ........... start insert mode at the end of the line
............. add one space
p ........... paste