我想要将yanked文本粘贴到Vim的命令行中。这可能吗?
是的。按Ctrl-R然后”。如果你有文字控制字符在你拉拽,使用Ctrl-R, Ctrl-O,”。
下面解释一下可以使用寄存器做些什么。您可以用寄存器做的事情是非凡的,一旦您知道如何使用它们,您就不能没有它们。
寄存器基本上是字符串的存储位置。Vim有许多以不同方式工作的寄存器:
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来显示所有寄存器的内容。该命令的同义词和简写为:display、:reg和:di。
在“插入”或“命令行”模式下,Ctrl-R加上一个寄存器名,插入该寄存器的内容。如果你想从字面上插入它们(没有自动缩进,没有控制字符的转换,如0x08到退格等),你可以使用Ctrl-R, Ctrl-O,寄存器名。 请参阅:help i_CTRL-R和以下段落以获得更多参考。
但是您还可以执行以下操作(我可能忘记了寄存器的许多用途)。
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_: 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 :@ and :help quote_quote 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 Corollary: Keep your search pattern but add an alternative: / Ctrl-R, / \|alternative. 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 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(). The reverse functions are getreg() and getregtype(). 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 @ 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 @ Don't confuse :@ (command that plays Vim commands from a register) and @ (normal-mode command that plays normal-mode commands from a register). 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 @: 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_= 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. Define a search pattern manually: :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. 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 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 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!). 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. Copy the entire buffer to the system clipboard: :%y+. cf. :help :range (for the % part) and :help :y. 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. Another, simpler, way of modifying a macro is to paste it in a buffer ("ap), edit it, and put it again into the register, by selecting it and "ay. 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. Even better, if you do "1P, then . in Normal mode will play "2P, and so on. cf. :help . and :help quote_number If you want to insert the current date in Insert mode: Ctrl-R=strftime('%y%m%d')Enter. cf. :help strftime()
再一次,让人困惑的是:
:@ 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. " is also a valid register name (the default, or unnamed, register) and therefore can be passed as an arguments for commands that expect register names
要将系统剪贴板中的内容粘贴到Vim命令行(“命令模式”),请使用Ctrl+R后跟+。对我来说,至少在Ubuntu上,Shift+Ins不起作用。
PS:我不确定为什么按Ctrl+R加*,理论上与按Ctrl+R加+一样,似乎并不总是有效。我搜索并发现了+版本,它似乎总是工作,至少在我的盒子。
还值得注意的是,yank寄存器与宏缓冲区是相同的。换句话说,您可以简单地在文档中写出整个命令(包括粘贴的代码片段),然后“通过将它拉到b寄存器,然后使用@b运行它”。
“我想把文本粘贴到Vim命令行中。”
虽然投票最多的答案非常完整,但我更喜欢编辑命令历史。
在正常模式下,输入:q:。这将为您提供一个最近命令的列表,可以用普通的vim命令编辑和搜索。您将从底部的空白命令行开始。
对于这篇文章所要求的事情,粘贴一个拉出的行(或任何东西)到命令行,拉出你的文本,然后:q:p(进入命令历史编辑模式,然后(p)将你拉出的文本放入一个新的命令行。随意编辑,输入即可执行。
要退出命令历史模式,则相反。在命令历史记录的普通模式下,输入::q + enter
我也遇到过类似的问题。我希望选择的文本以命令结尾,而不是依赖于粘贴它。下面是我试图为之写映射的命令:
:call VimuxRunCommand("python")
这个插件的文档只显示使用字符串文字。如果你试图选择包含双引号的文本,下面将会中断:
vnoremap y:call VimuxRunCommand("<c-r>"")<cr>
为了解决这个问题,你只需要使用@引用宏的内容:
vnoremap y:call VimuxRunCommand(@")<cr>
将未命名寄存器的内容传递进来,并使用双引号和多行边格。
OS X
如果你在Mac OS X上使用Vim,不幸的是它带有旧版本,并且不符合剪贴板选项。幸运的是,Homebrew可以轻松解决这个问题。
安装Vim:
编译安装vim——with-lua——with-override-system-vi
安装Vim的GUI版本:
安装macvim -with-lua -with-override-system-vi
重启终端生效。
将下面的行追加到~/.vimrc 设置剪贴板=不知名的
现在你可以用yy在Vim中复制这一行,并在系统范围内粘贴它。
作为上下文,这个信息来自开箱即用,没有插件,没有.vimrc Vim 7.4行为在Linux Mint的默认选项。
您总是可以用鼠标选择文本(或使用V或V并将选择放在“*寄存器”中),并使用Shift + Ctrl + V将其粘贴到命令行中。
在命令行中输入Ctrl + r将提示输入寄存器名称。因此键入:CTRL-r*将把内容寄存器*放到命令行中。它将粘贴任何寄存器,而不仅仅是“*”。参见:help c_CTRL-R。
此外,鼠标中间的按钮将粘贴到命令行中。
参见:help->quote-plus了解X窗口如何处理选择。即使是在普通的开箱即用的Vim中(同样是在Linux Mint中的Vim 7.4中),用鼠标左键所做的任何选择都可以用鼠标中键粘贴到命令行中。
此外,鼠标中间按钮还可以将在Vim中选择的文本粘贴到许多其他X Window应用程序中,甚至是GUI应用程序(例如Firefox和Thunderbird),并且可以将从其他应用程序中选择的文本粘贴到命令行中。
有关addl信息,请参阅:help->x11-selection。
博士tl;
首先尝试:Ctrl -r方法,然后使用Shift + Ctrl + v或如果需要其他方法,则使用鼠标中键。
人们承认这可能令人困惑。
如果你有两个值被插入到两个不同的寄存器(例如寄存器a和寄存器b),那么你可以简单地设置一个变量c并对其进行操作。
例如:set c = str2float(@a) + str2float(@b),然后你可以将c的内容粘贴到任何地方。
例如,在插入模式下,按CTRL + R,然后输入=进入表达式寄存器,在等号后输入c,然后按enter。现在你应该有了a和b寄存器的总数。
所有这些都可以记录在一个宏和重复!
如果使用浮点数,则使用str2float函数;如果不使用浮点数,则使用整数。
我不确定这是否是惯用的,但它适用于我的情况下,我需要在一行中添加2个数字,并重复500多行。