Vim:复制/设置多行,这些行只用一个vim命令中的某些文本分隔

时间:2022-09-11 19:18:57

Now for example, I have some text like (all on separate lines):

现在,例如,我有一些文字(所有在不同的行):

*

1st Line)Who are you?
2nd Line)How are you?
3rd Line)Where are you?
4th Line)Whom are you with?
5th Line)Why are you here?

*

What I want do is copy/yank line 1,3,5 in just one vim command and then paste it with the usual paste(p) command using vim.

我想要的是在一个vim命令中复制/抽取1,3,5行,然后使用vim将其粘贴到通常的paste(p)命令中。

4 个解决方案

#1


Here's one slightly manual way to do it, but one of the more slicker manual ways, I think. It can probably be automated with a recording or a function.

这是一种略微手动的方式,但我认为这是一种更为流畅的手动方式。它可以通过录音或功能自动化。

Position cursor on first line
"ayy
Position cursor on third line
"Ayy  (note the capital A)
Position cursor on fifth line
"Ayy  (again, capital).

Position cursor on place where you want to paste
p

The symbol " followed by a letter, before a command like yank, means to yank it into a "named buffer" (there are 26 named buffers I believe, one for each letter), and if you capitalize the letter then it's append, rather than overwrite, to that buffer.

符号“后跟一个字母,在像yank之类的命令之前,意味着把它拉到一个”命名缓冲区“(我相信有26个命名缓冲区,每个字母一个),如果你把这个字母大写,那么它就是附加的,而不是而不是覆盖,到那个缓冲区。

#2


Following would yank the uneven lines into register A.

接下来会将不均匀的线条拉到寄存器A中。

let @a=''|g/^/if (line('.')%2)|y A

Breakdown

  • let @a='' Clear the "a" register
  • 让@ a =''清除“a”寄存器

  • g/^/ for each line
  • 每行g / ^ /

  • if (line('.')%2) if the linenumber is uneven
  • if(line('。')%2)如果亚麻布不均匀

  • y A append it to register A
  • y A将其附加到注册A.

To copy immediately to the end of your file, you can use

要立即复制到文件末尾,您可以使用

g/^/if (line('.')%2)|co $

#3


You can do it with two command.

你可以用两个命令来完成它。

Option 1:

Search and highlight keywords in each line using your example.

使用您的示例搜索并突出显示每行中的关键字。

/\v1|3|5

Then use the command

然后使用该命令

:CopyLine

which can be found in the copymatches plugin

可以在copymatches插件中找到

Then paste with the p command.

然后使用p命令粘贴。

i mead a small video demo you can check it out.

我想一个小视频演示,你可以检查出来。

Option 2:

You can also do it using the Global Command

您也可以使用全局命令执行此操作

Fist Empty register A

拳头空注册A.

:let @a='' 

Then use the global command

然后使用global命令

:g/\%1l\|\%3l\|\%5l/y A

Then paste with the "ap command.

然后粘贴“ap命令。

#4


You can use a for loop to select the lines you are interested in using their line numbers.

您可以使用for循环使用其行号选择您感兴趣的行。

In your example, to yank lines 1,3,5 into register a the command is:

在您的示例中,要将1,3,5行放入寄存器,该命令为:

:let @a=''|for c in [1,3,5]|execute c . ',' . c . 'y A'|endfor

Then "a p will paste these 3 lines at the cursor position.

然后“a p将这3行粘贴到光标位置。

UPDATE: To include the paste operation in the command as well:

更新:要在命令中包含粘贴操作:

:let @a=''|for c in [1,3,5]|execute c . ',' . c . 'y A'|endfor|execute "normal! \"ap\<CR>"

#1


Here's one slightly manual way to do it, but one of the more slicker manual ways, I think. It can probably be automated with a recording or a function.

这是一种略微手动的方式,但我认为这是一种更为流畅的手动方式。它可以通过录音或功能自动化。

Position cursor on first line
"ayy
Position cursor on third line
"Ayy  (note the capital A)
Position cursor on fifth line
"Ayy  (again, capital).

Position cursor on place where you want to paste
p

The symbol " followed by a letter, before a command like yank, means to yank it into a "named buffer" (there are 26 named buffers I believe, one for each letter), and if you capitalize the letter then it's append, rather than overwrite, to that buffer.

符号“后跟一个字母,在像yank之类的命令之前,意味着把它拉到一个”命名缓冲区“(我相信有26个命名缓冲区,每个字母一个),如果你把这个字母大写,那么它就是附加的,而不是而不是覆盖,到那个缓冲区。

#2


Following would yank the uneven lines into register A.

接下来会将不均匀的线条拉到寄存器A中。

let @a=''|g/^/if (line('.')%2)|y A

Breakdown

  • let @a='' Clear the "a" register
  • 让@ a =''清除“a”寄存器

  • g/^/ for each line
  • 每行g / ^ /

  • if (line('.')%2) if the linenumber is uneven
  • if(line('。')%2)如果亚麻布不均匀

  • y A append it to register A
  • y A将其附加到注册A.

To copy immediately to the end of your file, you can use

要立即复制到文件末尾,您可以使用

g/^/if (line('.')%2)|co $

#3


You can do it with two command.

你可以用两个命令来完成它。

Option 1:

Search and highlight keywords in each line using your example.

使用您的示例搜索并突出显示每行中的关键字。

/\v1|3|5

Then use the command

然后使用该命令

:CopyLine

which can be found in the copymatches plugin

可以在copymatches插件中找到

Then paste with the p command.

然后使用p命令粘贴。

i mead a small video demo you can check it out.

我想一个小视频演示,你可以检查出来。

Option 2:

You can also do it using the Global Command

您也可以使用全局命令执行此操作

Fist Empty register A

拳头空注册A.

:let @a='' 

Then use the global command

然后使用global命令

:g/\%1l\|\%3l\|\%5l/y A

Then paste with the "ap command.

然后粘贴“ap命令。

#4


You can use a for loop to select the lines you are interested in using their line numbers.

您可以使用for循环使用其行号选择您感兴趣的行。

In your example, to yank lines 1,3,5 into register a the command is:

在您的示例中,要将1,3,5行放入寄存器,该命令为:

:let @a=''|for c in [1,3,5]|execute c . ',' . c . 'y A'|endfor

Then "a p will paste these 3 lines at the cursor position.

然后“a p将这3行粘贴到光标位置。

UPDATE: To include the paste operation in the command as well:

更新:要在命令中包含粘贴操作:

:let @a=''|for c in [1,3,5]|execute c . ',' . c . 'y A'|endfor|execute "normal! \"ap\<CR>"