如何在 Vim 中实现开箱即用的“粘贴 -d '␣'"行为? [英] How to achieve the “paste -d '␣'” behavior out of the box in Vim?

查看:46
本文介绍了如何在 Vim 中实现开箱即用的“粘贴 -d '␣'"行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vim 可以对 paste -d ' ' shell 的效果做些什么命令,然后通过 :r !paste -d ' '?

Can Vim do something to the effect of the paste -d ' ' shell command, other then by running it via :r !paste -d ' '?

可用于此目的的原生 Vim 命令有哪些(如果有)?

What are the native Vim commands that can be used for that, if any?

推荐答案

首先,让我们考虑一个有些不同但密切相关的问题:立即将一个范围的行附加到另一个范围在它之前.解决之后,我们将回到原来的状态答案的第二部分中的问题,并将展示如何原来的问题可以简化为建议的问题.

First, let us consider a somewhat different but closely related problem: appending one range of lines to another range immediately preceding it. After solving it, we will return to the original problem in the second part of the answer, and will show how the original problem can be reduced to the suggested one.

1. 在不限制一般性的情况下,我们假设第一个行块(要附加第二行的行)开始于缓冲区的第一行,并且光标位于最后一行第一个块的行.在这种情况下,可以使用连接线以下简短而有效的 Ex 命令:

1. Without restricting the generality, we assume that the first block of lines (the one to append the second one to) starts on the first line of the buffer, and that the cursor is located on the last line of that first block. In this case, the lines can be joined using the following short and efficient Ex command:

:1,g/^/''+m.|-j!

这个 :global 命令在从第一行到当前,依次执行两个 Ex 命令:''+m.-j!.前者,:move 命令,删除它旁边的行光标所在的位置,并将其插入到:global 命令当前正在处理的一个.后者,:join 命令,将刚移动的行附加到上面的行(不带添加或删除它们之间的空格,因为 ! 修饰符).

This :global command runs over the range of lines from the first to the current one, sequentially executing two Ex commands: ''+m. and -j!. The former, :move command, deletes the line next to that where the cursor has been positioned, and inserts it just below the one currently being processed by the :global command. The latter, :join command, appends the just moved line to the one above (without adding or removing whitespace between them, because of the ! modifier).

这些命令的构建利用了两个隐含的事实.首先,在执行 :global 中指定的命令之前在另一行,光标位于第一列那条线.这意味着被引用为 . 的地址对应于当前正在运行命令的最新行.第二,发送 :global 命令执行之前的光标位置是添加到跳转列表中.因此,该位置可以在范围通过 ' 伪标记(参见 :help :range).

The construction of these commands takes advantage of two implicit facts. First, before the command specified in a :global is executed on yet another line, the cursor is positioned at the first column of that line. It means that the address referenced as . corresponds to the latest line on which the command is currently being run. Second, the cursor position before sending a :global command to execution is added to the jump list. Therefore, that location can be addressed in ranges through the ' pseudo-mark (see :help :range).

如果需要在连接线之间放置一个分隔符,可以在执行 :join 之前添加一个替换命令:

If it is needed to put a separator in between joined lines, one can add a substitution command inserting it before :join is executed:

:1,g/^/''+m.|s/^/\t/|-j!

有一个默认Vim句子分离行为的选项当 :join 命令在没有 ! 修饰符的情况下运行时使用:

There is an option of the default Vim sentence separation behavior that is used when the :join command is run without the ! modifier:

:1,g/^/''+m.|-j

有关该空格分隔行为的详细信息,请参阅:help J:help :join,尤其是可以找到的段落:helpg 这些命令,除了gJ".

For details about that space-separation behavior, see :help J, :help :join, and especially the paragraph that can be found by :helpg These commands, except "gJ".

2.该技术很容易适用于所讨论的问题,因为最初的情况可以缩小到我们所拥有的情况以上考虑.为此,请转到包含行以追加和复制它们,

2. The technique is easily applicable to the problem in question, since the initial situation can be narrowed down to the one we have considered above. In order to do that, go to the buffer containing the lines to append and copy them,

:%y

然后,切换到包含要附加的文本的目标缓冲区,并将复制的行粘贴到缓冲区的当前内容下方,

Then, switch to the target buffer containing the text to append to, and paste the copied lines below the current contents of the buffer,

:$pu|'[-

上面的命令结合了两个动作:

The above command combines two actions:

  1. 将未命名寄存器的内容粘贴到最后一行下方,将光标移动到粘贴文本的最后一行.
  2. 将光标移动到粘贴前的最后一行.

在此基础上,可以使用之前提出的 :global 命令之一立即地.当然,也可以同时发布粘贴和在一次运行中转换:

Upon that, one of the :global commands proposed earlier can be used immediately. Of course, it is possible to issue both pasting and transforming in a single run:

:$pu|'[-|1,g/^/''+m.|s/^/\t/|-j!

这篇关于如何在 Vim 中实现开箱即用的“粘贴 -d '␣'"行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆