tags:

views:

161

answers:

1

I'd like to paste yanked text into a vim colon command. Is this possible?

+20  A: 

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

Here is a whole explanation about 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 stores what you yank or delete and other strings into registers:

  • registers a to z for your own use, but also
  • 0 (yank register),
  • 1 to 9 (delete registers),
  • _ (like /dev/null, this is a black hole),
  • " (default register, hence the CTRL-R "),
  • - (small delete register),
  • / (the search pattern register),
  • : (last command register)

among others. see :help registers for full reference.

In insert or command-line mode, Ctrl-R plus a register name, inserts the contents of this register. If you want to insert them literaly, 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 must forget many uses of registers)

  • to copy last command into a buffer in normal mode: ":p (note that " lets you select what register you will use for the next yank, delete or paste operation).
  • to run ex command that you just yanked (with yy for example), run :@"
  • to insert last search pattern into a buffer in insert mode: CTRL-R /
  • to start a new line containing last yanked text in normal mode, if it was not yanked linewise: :put " ENTER. Here, " is the register name, not the command from first example.
  • if you have in the system clipboard a sequence of keys that you should run in normal mode, use @+ or @*to play them. The @ command, used to play macros recorded with q, will act as if you had typed in normal mode the sequence of keys from the register that you specify.
  • if you have recorded a macro with qa...q, then :echo @a will tell you what you have typed.
  • if you want to manually define your search pattern, then do :let @/ = 'foo'
  • if you want to show what you will be doing in normal mode before running it: do @='dd'ENTER : this will evaluate the expression register, which will contain then dd, and therefore delete current line.
  • if you want to copy to clipboard all lines beginning with foo, and afterwards all lines beginning with bar, do :g/^foo/y a, :g/^bar/y A, :let @+ = @a. 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.

Reference:

  • :help "
  • :help :@
  • :help i_CTRL-R
  • :help :put
  • :help @
  • :help q
  • :help :let
  • :help @=
Benoit
Isn't there a badge “100/1 length ratio between answer and question?” :)
Benoit
+1 for this great register explanation! I will use it!
levif
It's a great answer, but it's about 900 words short of 1400. :)
Roger Pate
Yes. But it is still possible to expand the answer :)
Benoit