VIM可以做这个Sublime技巧吗?

时间:2018-02-28 14:59:55

标签: vim

我想编辑此文:

Aenean vel sem bibendum, eleifend odio a, dictum nibh.
The third word in the above line is 
Morbi eget est vitae quam ultricies porta vitae quis nunc.
The third word in the above line is 
Praesent accumsan ex a laoreet cursus.
The third word in the above line is 
Quisque efficitur lectus at dolor mollis, sit amet tristique libero lobortis.
The third word in the above line is 

到本文:

Aenean vel sem bibendum, eleifend odio a, dictum nibh.
The third word in the above line is sem
Morbi eget est vitae quam ultricies porta vitae quis nunc.
The third word in the above line is est
Praesent accumsan ex a laoreet cursus.
The third word in the above line is ex
Quisque efficitur lectus at dolor mollis, sit amet tristique libero lobortis.
The third word in the above line is lectus

使用Sublime

执行此操作
Select repeated part
ALT+F3
UpArrow
HOME
3 x CTRL+RightArrow
CTRL+Shift+LeftArrow
CTRL+C
DownArrow
END
CTRL+V

这个技巧在很多情况下非常有用。 VIM能做到吗?

3 个答案:

答案 0 :(得分:9)

它应该可以轻松使用宏。如果您将光标放在第一行的第一个字符上,那么:

qq:开始录制名为q

的宏

2w:提前两个字

yw:猛拉(复制)字

j$:跳到下一行并转到行尾

p:粘贴你已经抽出的东西

+:转到下一行的开头

q:停止录制宏。

3@q:执行名为q 3次的宏

答案 1 :(得分:1)

作为宏解决方案的替代方案,您还可以使用替换命令

:%s/\vis $\zs/\=split(getline(line('.')-1), ' ')[2]

<强>击穿

:%s/                                      start a substitute command
\vis $\zs/                                select lines ending with "is "
                                          use \zs to start replacing after the match
\=split(getline(line('.')-1), ' ')[2]     split previous line on spaces and use the 3th item from the list

请注意,要将此作为一般模板使用,必须按住

  • 您需要一个搜索条件才能匹配您想要更改的行。对于您的示例,这是is $
  • 您需要能够在有意义的事物上拆分前一行,并在每个替换的相同数组位置返回您需要的项目。例如,这是通过拆分空间并返回第3项。

答案 2 :(得分:1)

另一个vim解决方案:

:g/^/if line('.') % 2 | normal! wwyiwj$p | endif


g .................. globally
/^/ ................ on every start of line
if
line('.') % 2 ...... Mod of the integer division of line number by 2
normal! ............ normal mode
ww ................. jump to the third word
yiw ................ yank inner word (word under cursor)
j .................. go to the line below
$ .................. jump to othe end of the line
p .................. paste