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

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

问题描述

我想将提取的文本粘贴到 Vim 的命令行中.可能吗?

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

推荐答案

是的.按 Ctrl-R 然后按 ".如果您在猛拉的内容中有文字控制字符,请使用 Ctrl-RCtrl-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 to 9(移位删除寄存器,当你使用cd等命令时,已删除进入寄存器 1,寄存器 1 中的内容进入寄存器 2,),
  • "(默认寄存器,也称为未命名寄存器.这是 "Ctrl-R, "),
  • az 供您自己使用(大写的 AZ 用于附加到相应的寄存器).
  • _(就像 /dev/null (Unix) 或 NUL (Windows),你可以写它但它被丢弃了,当你读取它时,它总是空的),
  • -(小删除寄存器),
  • /(搜索模式寄存器,当您使用 /?*# 例如;您也可以写入它以动态更改搜索模式),
  • :(通过 Q: 存储最后输入的 VimL 命令,只读),
  • +*(系统剪贴板寄存器,可以写入它们来设置剪贴板并从中读取剪贴板内容)
  • 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)

请参阅 :help registers 以获取完整参考.

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 中使用的最后一个命令被粘贴到你的缓冲区中.
    让我们分解一下:" 是一个普通模式命令,它可以让您选择在下一次 yank、delete 或 paste 操作中要使用的寄存器.所以 ": 选择冒号寄存器(存储最后一条命令).然后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.

参见:help ", :help quote_:

您正在编辑一个 VimL 文件(例如您的 .vimrc)并希望立即执行几个连续的行:yj:@"回车.
在这里,yj 将当前行和下一行(这是因为 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.

参见:help :@:help quote_quote

使用 Ctrl-R/.

参见: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.

参见:help :put

你也可以这样做::call setreg('"', @", 'V') 然后 p.setreg 函数设置其名称作为第一个参数(作为字符串)给出的寄存器,使用第二个参数的内容对其进行初始化(并且您可以将寄存器用作名称为 @x 其中 x 是 VimL 中的寄存器名称),并将其转换为第三个参数中指定的模式,V 为 linewise, nothing for characterwise和文字 ^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.

参见: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)

参见:help q, help@

上例的推论:如果剪贴板中有 8go,则 @+ 会将剪贴板内容作为宏播放,从而转到第 8 个字节你的文件.实际上,这几乎适用于所有寄存器.如果最后插入的字符串是插入模式下的 dd,则 @. 将(因为 . 寄存器包含最后插入的字符串)删除一行.(Vim 文档在这方面是错误的,因为它声明寄存器 #%:.仅适用于 pP:putCtrl-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).

参见: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.

参见:help@:

在运行之前显示您将在正常模式下执行的操作:@='dd' Enter.只要你按下 = 键,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.

参见: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'

参见: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, <代码>:让@+ = @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 命令",然后通过键入 visual 返回普通模式.

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.

参见: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+.

参见: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 您可以获得最后删除的行.但实际上你也可以使用寄存器 @1@9 恢复多达 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.

参见: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.

参见: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).

" 在正常模式命令中帮助您选择一个寄存器以进行 yank、粘贴、删除、更正等.

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

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

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