如何在vim中将字符串中的单词编织成字符串块?

如何在vim中将字符串中的单词编织成字符串块?

给定一个字符串和一个字符串块,例如

细绳:

Use three words.

堵塞:

This is the first string of another block of strings.
This is the second string of another block of strings.
This is the third string of another block of strings.

现在我想逐行连接/编织字符串和块,使新块看起来像这样:

This is the first string of another block of strings.
Use

This is the second string of another block of strings.
three

This is the third string of another block of strings.
words.

到目前为止我所做的是

:'<,'>s/\s/\r\r\r

其中'<,'>s是跨越字符串 的范围Use three words.。这将为我提供新行中字符串的每个单词:

Use


three


words.

然后我Ctrl+v选择该块,复制并粘贴它,这样我得到:

This is the first string of another block of strings.
Use
     This is the second string of another block of strings.
three
     This is the third string of another block of strings.
words.

我手动将它变成我需要的形状,并使用了很多 ifv和用法。wx

如何通过 vim 中的简单复制和粘贴指令更有效地完成此操作?

答案1

您可以通过交换粘贴从剪切缓冲区中直接传送文本 - 粘贴到选择交换中,因此dwVP行删除除删除的单词之外的所有内容。

从...开始

Use three words.
This is the first string of another block of strings.
This is the second string of another block of strings.
This is the third string of another block of strings.

并做

:normal ggdd    " three-word line in the cut buffer, cursor on first "This" line
:normal pdwVPo<ESC>j  " dwVP is cut a word and exchange-paste it back for the rest of the line
:normal pdwVPo<ESC>j  " do it again
:normal pdwVPo<ESC>j  " again

对于只有三个我不会QQ,但ggddqqpdwVPo<ESC>jq@q@@更短。

答案2

我会像你一样开始将句子分成新行,但之后我会

  • 为两个块的每一行编号
  • 将一个部分完全粘贴到另一部分
  • 按数字排序
  • 删除数字

这可能看起来像这样

  • 布洛克1-:%s/\s/\r/g
  • 布洛克1-%s/^/\=line('.')*1000+1
  • 布洛克2-%s/^/\=line('.')*1000
  • 复制整个布洛克2进入缓冲区布洛克1
  • sort n
  • %s/\v^\d+

相关内容