VIM可以做这个Sublime技巧吗?

问题描述 投票:3回答:3

我想编辑此文本:

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能做到吗?

vim
3个回答
9
投票

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

qq:开始录制名为q的宏

2w:提前两个字

yw:yank(复制)字

j$:跳到下一行并走到最后

p:粘贴你所拉的东西

+:开始下一行

q:停止录制宏。

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


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个项目。

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
© www.soinside.com 2019 - 2024. All rights reserved.