如何将粘贴的文本粘贴到Vim命令行中 [英] How to paste yanked text into the Vim command line

查看:130
本文介绍了如何将粘贴的文本粘贴到Vim命令行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将选中的文本粘贴到Vim的命令行中。

I'd like to paste yanked text into Vim's command line. Is it possible?

推荐答案

是。依次按 Ctrl - R 。如果要删除的内容中包含文字控制字符,请使用 Ctrl - R Ctrl - O

Yes. Hit Ctrl-R then ". If you have literal control characters in what you have yanked, use Ctrl-R, Ctrl-O, ".

在此说明您可以使用寄存器进行的操作。您可以对寄存器执行的操作非同寻常,一旦您知道如何使用它们,就不能没有它们。

Here is an explanation of what you can do with registers. What you can do with registers is extraordinary, and once you know how to use them you cannot live without them.

寄存器基本上是字符串的存储位置。 Vim有许多以不同方式工作的寄存器:

Registers are basically storage locations for strings. Vim has many registers that work in different ways:


  • 0 (猛拉寄存器:当您在普通模式下使用 y 而不指定寄存器时,带引号的文本也会到达默认寄存器),

  • 1 9 (在使用诸如 c d ,已删除的内容将进入寄存器1,寄存器1中的内容将进入寄存器2,等。),

  • (默认寄存器,也称为未命名寄存器。这是 出现在 Ctrl - R ),

  • a 转换为 z 以供自己使用(将 A 大写为 Z
  • _ (类似于 / dev / null (Unix)或 NUL (Windows),您可以对其进行写入,但是会被丢弃,并且当您从中读取时,它始终为空),

  • -(小删除寄存器),

  • / (搜索模式寄存器,当您使用 / 查找文本时更新*例如;您也可以写它来动态更改搜索模式),

  • (通过<$ c $存储最后一个VimL类型的命令c> Q ,只读),

  • + * (系统剪贴板寄存器,您可以写信给他们设置剪贴板并从中读取剪贴板内容)

  • 0 (yank register: when you use y in normal mode, without specifying a register, yanked text goes there and also to the default register),
  • 1 to 9 (shifting delete registers, when you use commands such as c or d, what has been deleted goes to register 1, what was in register 1 goes to register 2, etc.),
  • " (default register, also known as unnamed register. This is where the " comes in Ctrl-R, "),
  • a to z for your own use (capitalized A to Z are for appending to corresponding registers).
  • _ (acts like /dev/null (Unix) or NUL (Windows), you can write to it but it's discarded and when you read from it, it is always empty),
  • - (small delete register),
  • / (search pattern register, updated when you look for text with /, ?, * or # for instance; you can also write to it to dynamically change the search pattern),
  • : (stores last VimL typed command via Q or :, readonly),
  • + and * (system clipboard registers, you can write to them to set the clipboard and read the clipboard contents from them)

请参见:帮助注册以获取完整参考。

See :help registers for the full reference.

您随时可以使用:registers 显示所有寄存器的内容。此命令的同义词和缩写为:display :reg :di

You can, at any moment, use :registers to display the contents of all registers. Synonyms and shorthands for this command are :display, :reg and :di.

在插入或命令行模式下, Ctrl - R 加上寄存器名称,插入该寄存器的内容。如果要按原样插入它们(不自动缩进,不将控制字符如 0x08 转换为退格键等),则可以使用 Ctrl - R Ctrl - O ,寄存器名称。
请参阅:help i_CTRL-R 和以下段落以获取更多参考。

In Insert or Command-line mode, Ctrl-R plus a register name, inserts the contents of this register. If you want to insert them literally (no auto-indenting, no conversion of control characters like 0x08 to backspace, etc), you can use Ctrl-R, Ctrl-O, register name. See :help i_CTRL-R and following paragraphs for more reference.

但是您也可以以下内容(我可能忘记了寄存器的许多用途)。

But you can also do the following (and I probably forgot many uses for registers).


  • 在正常模式下,按 p 。您在vim中使用的最后一条命令将粘贴到缓冲区中。

    让我们分解: 是普通模式命令,可让您选择在下一次拉动,删除或粘贴操作期间要使用的寄存器。因此, 选择冒号寄存器(存储最后一个命令)。然后 p 是您已经知道的命令,它将粘贴寄存器的内容。

  • In normal mode, hit ":p. The last command you used in vim is pasted into your buffer.
    Let's decompose: " is a Normal mode command that lets you select what register is to be used during the next yank, delete or paste operation. So ": selects the colon register (storing last command). Then p is a command you already know, it pastes the contents of the register.

cf。:help :help quote _:

您正在编辑VimL文件(例如,您的 .vimrc ),立即执行几行连续的代码: y j @ Enter

在这里, y j 拖动当前行和下一行(这是因为j是沿行方向运动,但是超出此答案的范围)到默认寄存器(也称为未命名寄存器)中,然后:@ Ex命令播放作为参数给定的寄存器中存储的Ex命令,以及 是您引用未命名寄存器的方式。

You're editing a VimL file (for instance your .vimrc) and would like to execute a couple of consecutive lines right now: yj:@"Enter.
Here, yj yanks current and next line (this is because j is a linewise motion but this is out of scope of this answer) into the default register (also known as the unnamed register). Then the :@ Ex command plays Ex commands stored in the register given as argument, and " is how you refer to the unnamed register. Also see the top of this answer, which is related.

不要混淆此处使用的 (这是一个寄存器名称)与上例中的 相同,这是普通模式命令。

Do not confuse " used here (which is a register name) with the " from the previous example, which was a Normal-mode command.

cf。 :help:@ :help quote_quote

使用 Ctrl - R / ,在插入模式下或在命令行中将最后一个搜索模式插入文件中。

Insert the last search pattern into your file in Insert mode, or into the command line, with Ctrl-R, /.

cf。 :help quote _ / help i_CTRL-R

推论:保留您的搜索模式,然后添加一个替代方案: / Ctrl - R / \ |替代

Corollary: Keep your search pattern but add an alternative: / Ctrl-R, / \|alternative.

您已在以下行的中间选择了两个单词可视模式,用 y 将其拉出,它们处于未命名的寄存器中。现在,您要在您所在的位置下方打开一个新行,并输入以下两个单词::pu 。这是:put 的简写。:put 命令像许多Ex命令一样,仅沿行方向工作。

You've selected two words in the middle of a line in visual mode, yanked them with y, they are in the unnamed register. Now you want to open a new line just below where you are, with those two words: :pu. This is shorthand for :put ". The :put command, like many Ex commands, works only linewise.

cf。:help:put

您也可以完成以下操作::调用setreg('',@,'V')然后 p 。 c $ c> setreg 函数设置名称为第一个参数(作为字符串)的寄存器,并使用第二个参数的内容对其进行初始化(您可以将寄存器用作带有名称 @x ,其中 x 是VimL中的寄存器名称),并将其转换为第三个参数指定的模式, V 表示行方向,字符型和文字 ^ V 表示块级。

You could also have done: :call setreg('"', @", 'V') then p. The setreg function sets the register of which the name is given as first argument (as a string), initializes it with the contents of the second argument (and you can use registers as variables with the name @x where x is the register name in VimL), and turns it into the mode specified in the third argument, V for linewise, nothing for characterwise and literal ^V for blockwise.

cf。:help setreg()。反向函数是 getreg() getregtype()

cf. :help setreg(). The reverse functions are getreg() and getregtype().

如果您记录了带有 qa ... q ,然后:echo @a 会告诉您您键入的内容,并且 @a 将重播宏(可能您知道一个宏,它对于避免重复任务非常有用)

If you have recorded a macro with qa...q, then :echo @a will tell you what you have typed, and @a will replay the macro (probably you knew that one, very useful in order to avoid repetitive tasks)

cf. :help q help @

上一个示例:如果剪贴板中有 8go ,则 @ + 将宏作为剪贴板内容播放,并且因此转到文件的第8个字节。实际上,这几乎适用于每个寄存器。如果在插入模式下最后插入的字符串是 dd ,则 @。将(因为寄存器包含最后插入的字符串)删除一行。 (Vim文档在这方面是错误的,因为它指出寄存器仅适用于 p P :put Ctrl - R )。

Corollary from the previous example: If you have 8go in the clipboard, then @+ will play the clipboard contents as a macro, and thus go to the 8th byte of your file. Actually this will work with almost every register. If your last inserted string was dd in Insert mode, then @. will (because the . register contains the last inserted string) delete a line. (Vim documentation is wrong in this regard, since it states that the registers #, %, : and . will only work with p, P, :put and Ctrl-R).

cf。 :help @

不要混淆:@ (从寄存器播放Vim命令的命令)和 @ (从寄存器播放普通模式命令的普通模式命令)。

Don't confuse :@ (command that plays Vim commands from a register) and @ (normal-mode command that plays normal-mode commands from a register).

值得注意的例外是 @:。命令寄存器不包含初始冒号,也不包含最终回车符。但是,在普通模式下, @:会执行您期望的操作,将寄存器解释为Ex命令,而不是尝试在普通模式下播放。因此,如果您的最后一个命令是:e ,则寄存器包含 e @:将重新加载文件,而不是字尾。

Notable exception is @:. The command register does not contain the initial colon neither does it contain the final carriage return. However in Normal mode, @: will do what you expect, interpreting the register as an Ex command, not trying to play it in Normal mode. So if your last command was :e, the register contains e but @: will reload the file, not go to end of word.

cf。 :help @:

在运行之前显示在普通模式下的操作: @ ='dd' 输入。按下 = 键后,Vim就会切换到表达式求值:输入表达式并按下 Enter 时,Vim会对其进行计算。结果充当寄存器内容。当然,寄存器 = 是只读的,只有一次。每次开始使用它时,都必须输入一个新表达式。

Show what you will be doing in Normal mode before running it: @='dd' Enter. As soon as you hit the = key, Vim switches to expression evaluation: as you enter an expression and hit Enter, Vim computes it, and the result acts as a register content. Of course the register = is read-only, and one-shot. Each time you start using it, you will have to enter a new expression.

cf。 :help quote _ =

推论:如果您正在编辑命令,并且意识到应该插入从当前缓冲区的命令行进入命令行:不要按 Esc !使用 Ctrl - R = getline(58) Enter 。之后,您将返回命令行编辑,但是它已经插入了第58行的内容。

Corollary: If you are editing a command, and you realize that you should need to insert into your command line some line from your current buffer: don't press Esc! Use Ctrl-R =getline(58) Enter. After that you will be back to command line editing, but it has inserted the contents of the 58th line.

手动定义搜索模式::let @ / ='foo'

cf。 :help:let

请注意,这样做不必逃避 / 。当然,您需要将所有单引号加倍。

Note that doing that, you needn't to escape / in the pattern. However you need to double all single quotes of course.

复制以 foo 开头的所有行,然后然后将所有包含 bar 的行粘贴到剪贴板,将这些命令链接起来: qaq (重置 a 寄存器中存储一个空宏),:g / ^ foo / y A :g / bar / y A :let @ + = @a

Copy all lines beginning with foo, and afterwards all lines containing bar to clipboard, chain these commands: qaq (resets the a register storing an empty macro inside it), :g/^foo/y A, :g/bar/y A, :let @+ = @a.

使用大写的寄存器名称可以使寄存器在追加模式下工作

Using a capital register name makes the register work in append mode

更好的是,如果 Q 没有被 mswin.vim ,使用 Q 启动Ex模式,链接那些实际上更好地称为 Ex命令的冒号命令,然后通过键入<$ c返回普通模式$ c>视觉

Better, if Q has not been remapped by mswin.vim, start Ex mode with Q, chain those "colon commands" which are actually better called "Ex commands", and go back to Normal mode by typing visual.

cf。 :help:g :help:y :help Q

对文件进行两次换行::g / ^ / put _ 。这会将黑洞寄存器的内容(读取时为空,但可写,表现为 / dev / null ),每行之后(因为每行都有一个开始!) )。

Double-space your file: :g/^/put _. This puts the contents of the black hole register (empty when reading, but writable, behaving like /dev/null) linewise, after each line (because every line has a beginning!).

在每行之前添加包含 foo 的行::g / ^ /-put ='foo'。这是表达式寄存器的巧妙用法。在这里,- .- 1 的同义词(参见:help:range )。由于:put 将文本放在行后,因此您必须明确地告诉它要对前一个文本执行操作。

Add a line containing foo before each line: :g/^/-put ='foo'. This is a clever use of the expression register. Here, - is a synonym for .-1 (cf. :help :range). Since :put puts the text after the line, you have to explicitly tell it to act on the previous one.

将整个缓冲区复制到系统剪贴板::%y +

Copy the entire buffer to the system clipboard: :%y+.

cf。 :help:range (用于部分)和:help:y

cf. :help :range (for the % part) and :help :y.

如果您录制了宏错误,则可以键入:let @a =' Ctrl - R = replace(@a,',,'g') 输入 '并进行编辑。这将修改存储在寄存器 a 中的宏的内容,并在此处显示如何使用表达式寄存器来执行此操作。

If you have misrecorded a macro, you can type :let @a=' Ctrl-R =replace(@a,"'","''",'g') Enter ' and edit it. This will modify the contents of the macro stored in register a, and it's shown here how you can use the expression register to do that.

如果您执行了 dddd ,则可以执行 uu 以撤消操作。使用 p ,您可以获取最后删除的行。但是实际上,您还可以通过 @ 9 通过寄存器 @ 1 恢复多达9个删除操作。

If you did dddd, you might do uu in order to undo. With p you could get the last deleted line. But actually you can also recover up to 9 deletes with the registers @1 through @9.

更好的是,如果您执行 1P ,然后执行将播放 2P ,依此类推。

Even better, if you do "1P, then . in Normal mode will play "2P, and so on.

cf。 :help。:help quote_number

如果要在插入模式下插入当前日期: Ctrl - R = strftime('%y%m%d') 输入

If you want to insert the current date in Insert mode: Ctrl-R=strftime('%y%m%d')Enter.

cf。 :help strftime()

再一次,可以令人困惑:

Once again, what can be confusing:


  • :@ 是解释内容的命令行命令寄存器作为vimscript并以普通模式获取

  • @ ,以普通模式将寄存器的内容解释为-模式击键(使用寄存器时除外,该寄存器包含最后播放的命令,但没有初始冒号:在这种情况下,它将重播命令,就像您也重新键入了冒号并(最后一个返回键)。

  • :@ is a command-line command that interprets the contents of a register as vimscript and sources it
  • @ in normal mode command that interprets the contents of a register as normal-mode keystrokes (except when you use : register, that contains last played command without the initial colon: in this case it replays the command as if you also re-typed the colon and the final return key).

可以帮助您选择要删除的寄存器,粘贴,删除,更正等。

" in normal mode command that helps you select a register for yank, paste, delete, correct, etc.

这篇关于如何将粘贴的文本粘贴到Vim命令行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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