How to paste yanked text into the Vim command line
Solution 1
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.
Registers are basically storage locations for strings. Vim has many registers that work in different ways:
0
(yank register: when you usey
in normal mode, without specifying a register, yanked text goes there and also to the default register),1
to9
(shifting delete registers, when you use commands such asc
ord
, 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
toz
for your own use (capitalizedA
toZ
are for appending to corresponding registers)._
(acts like/dev/null
(Unix) orNUL
(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 viaQ
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.
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
.
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).
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')
thenp
. Thesetreg
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
wherex
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 aregetreg()
andgetregtype()
.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 wasdd
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 withp
,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 containse
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 containingbar
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 bymswin.vim
, start Ex mode withQ
, chain those “colon commands” which are actually better called “Ex commands”, and go back to Normal mode by typingvisual
.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 registera
, 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 douu
in order to undo. Withp
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()
Once again, what can be confusing:
:@
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
Solution 2
For pasting something that is the system clipboard you can just use SHIFT - INS.
It works in Windows, but I am guessing it works well in Linux too.
Solution 3
"I'd like to paste yanked text into Vim command line."
While the top voted answer is very complete, I prefer editing the command history.
In normal mode, type: q:
. This will give you a list of recent commands, editable and searchable with normal vim commands. You'll start on a blank command line at the bottom.
For the exact thing that the article asks, pasting a yanked line (or yanked anything) into a command line, yank your text and then: q:p
(get into command history edit mode, and then (p)ut your yanked text into a new command line. Edit at will, enter to execute.
To get out of command history mode, it's the opposite. In normal mode in command history, type: :q
+ enter
Solution 4
For pasting something from the system clipboard into the Vim command line ("command mode"), use Ctrl+R followed by +. For me, at least on Ubuntu, Shift+Ins is not working.
PS: I am not sure why Ctrl+R followed by *, which is theoretically the same as Ctrl+R followed by + doesn't seem to work always. I searched and discovered the + version and it seems to work always, at least on my box.
Solution 5
It's worth noting also that the yank registers are the same as the macro buffers. In other words, you can simply write out your whole command in your document (including your pasted snippet), then "by
to yank it to the b
register, and then run it with @b
.

dan
Updated on May 13, 2021Comments
-
dan over 1 year
I'd like to paste yanked text into Vim's command line. Is it possible?
-
Benoit about 12 yearsIsn't there a badge “100/1 length ratio between answer and question?” :)
-
Admin about 12 yearsIt's a great answer, but it's about 900 words short of 1400. :)
-
Benoit about 12 yearsYes. But it is still possible to expand the answer :)
-
Myer over 11 yearsThis answer has changed the way I use vim. Thanks Benoit
-
Myer over 11 years@Benoit - It looks pretty good this way; I don't quite remember the way it was before. I read this a while ago, and I wrote my comment because I realized that I had already changed the way I used vim because of reading your answer a while back.
-
Pedro Luz over 11 yearsFor a better understanding read the 42 pdf in this url zmievski.org/talks
-
Teoulas about 11 yearsYup, works in Linux too. Tested under GNOME in both Vim and GVim.
-
idbrii over 10 yearsShouldn't
:g/^foo/y a
be:g/^foo/y A
to copy more than just the last line matching foo? (And then you may want to prependlet @a = @_
to clear the register.) Or am I missing something with the way you'd chain these commands? I assume that means to enter them one after the other if not using Ex mode. Even using Ex mode (withQ
), typing them in one after the other doesn't work. -
Benoit over 10 years@pydave: You're right. Clearing the register can also be done with
qaq
. -
lericson over 10 yearsThis actually feeds the pasteboard to Vim as keyboard input, so if you're in normal mode and paste
:!rm -rf /<cr>
, it'd execute that. Use registers instead! -
skeept over 10 years@lericson Didn't I say in my answer that this useful for something that is in the system keyboard? I don't understand the downvote. And for you example, seriously? You really have that in the clipboard? And you don't think that typing
shift-ins
is easier thanctrl-R+*
? This is of course if you are in gvim. If you are in the terminal connected to another machine and you want to paste something from your clipboard do you know something better thanshift-ins
? because the registers don't work in that situation. Please think before you comment and do other things (like downvoting). -
lericson almost 10 yearsI think it is misinformation and I have acted on that judgement.
-
skeept almost 10 yearsDid you actually read and understand the part that you may not be able to paste using the registers and then this would be a good option? This question complements the accepted answer.
-
eugenevd over 9 yearsDont forget :reg which will list all registers along with their content
-
laike9m over 8 yearsThis is just what I was looking for!
-
kd8azz about 8 yearsI think your last example is missing a ". Perhaps you meant VimuxRunCommand("@").
-
Conrad.Dean almost 8 yearsThat would send the
@
character to the vimux command. think of@
as escaping the following double quote and it being treated as a register instead of as a regular character. Thevnoremap
i've defined is active in visual mode, so it yanks the selected text, and then pastes it into the vimux command, sending it to a running interpreter in an adjacent pane. really great for working on a script and testing small portions of it with a couple keystrokes. -
Clay about 7 yearsif you are already modifying an existing command and did not press 'q:' press ctrl+f and it'll pull up the ability to edit the command history.
-
Christian Long almost 7 yearsIn recent versions of vim, deletions that are smaller than one line go in to the "small deletions register" instead of going in to the numbered registers. References: Vi and Vim, Vi and Vim, Reddit
-
Matias Thayer about 6 yearsThe difference between Ctrl-r+ and Ctrl-r* is that the former is the regular clipboard, but in Linux there is a second type of clipboard called "primary" that represents the most recently selected text (the middle button mouse paste from it). This is not available in Windows or MAC though
-
Marcel almost 5 yearsThere is also a very useful
.
register. Great write up though. -
shmup almost 5 yearsRegarding shifting delete registers, do not forget this uh, design decision:
:help "1
- Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). -
Max Waterman over 3 yearsDoesn't work for me. I just get '<S-Insert>' added to my command line. Perhaps I'm missing something. (Ubuntu 19.04/2:8.1.0320-1ubuntu3.1).
-
magras almost 3 years
S-Insert
works only on certain terminal emulators. And as @lericson properly said it will literally drop content of your clipboard as if it was typed by user. Without suggesting to enterinsert
mode before pressingS-Insert
, this answer is at least incomplete. -
Peter Mortensen over 2 yearsWhat is meant by "It used to CentOS 7."?
-
patrick over 1 yearAlso note that you still have to enter the command line using
:
before doing the CTRL+R command -
young_souvlaki 10 monthsIs there a way to escape regex special characters similar to
C-O
?