How to Paste from Buffer in Ex Mode of Vim

How to paste from buffer in ex mode of vim?

I understand that you want to:

  1. yank the current line and the two lines below in the current buffer,
  2. open an empty buffer in a new horizontal split and
  3. paste those three lines in the empty buffer.

Is that correct?

What I don't get is why you would want to do it from Ex mode while it's so easy (and working) in normal mode:

3yy
:new<cr>
p

I think that you are confusing ex mode, accessible with Q and command mode, accessible with :. You probably also confuse the :p[rint] command and the :pu[t] command.

Do the following from normal mode:

:.,+2y|new|put!

It may be helpful to know that you can also directly write those three lines to a file with:

:.,+2w filename

How to copy command string in the ex mode into clipboard?

You can turn the command-line mode into a full Vim buffer named the command-line window by pressing <C-f>, or enter it directly via q:. There, you can yank as usual; e.g. via "+yy or :yank +<CR>. (This is also great for other complex edits!)

The last command-line is in register :; :let @+ = @:<CR> will copy that to the clipboard.

The least Vim-like approach would be selecting with the mouse; on Linux, the text is then in the X primary selection; on Windows, you can then use Edit > Copy to put it in the clipboard.

How to paste yanked text into the Vim command line

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 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.

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') 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()

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

How to make vim paste from (and copy to) system's clipboard?

Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard') returns 0. In this case, vim is not compiled with the +clipboard feature and you'll have to install a different version or recompile it. Some linux distros supply a minimal vim installation by default, but if you install the vim-gtk or vim-gtk3 package you can get the extra features nonetheless.

The "* and "+ registers are for the system's clipboard (:help registers). Depending on your system, they may do different things. For instance, on systems that don't use X11 like OSX or Windows, the "* register is used to read and write to the system clipboard. On X11 systems both registers can be used. See :help x11-selection for more details, but basically the "* is analogous to X11's _PRIMARY_ selection (which usually copies things you select with the mouse and pastes with the middle mouse button) and "+ is analogous to X11's _CLIPBOARD_ selection (which is the clipboard proper).

If all that went over your head, try using "*yy or "+yy to copy a line to your system's clipboard. Assuming you have the appropriate compile options, one or the other should work.

You might like to remap this to something more convenient for you. For example, you could put vnoremap <C-c> "*y in your ~/.vimrc so that you can visually select and press Ctrl+c to yank to your system's clipboard.

You also may want to have a look at the 'clipboard' option described in :help cb. In this case you can :set clipboard=unnamed or :set clipboard=unnamedplus to make all yanking/deleting operations automatically copy to the system clipboard. This could be an inconvenience in some cases where you are storing something else in the clipboard as it will override it.

To paste you can use "+p or "*p (again, depending on your system and/or desired selection) or you can map these to something else. I type them explicitly, but I often find myself in insert mode. If you're in insert mode you can still paste them with proper indentation by using <C-r><C-p>* or <C-r><C-p>+. See :help i_CTRL-R_CTRL-P.

It's also worth mentioning vim's paste option (:help paste). This puts vim into a special "paste mode" that disables several other options, allowing you to easily paste into vim using your terminal emulator's or multiplexer's familiar paste shortcut. (Simply type :set paste to enable it, paste your content and then type :set nopaste to disable it.) Alternatively, you can use the pastetoggle option to set a keycode that toggles the mode (:help pastetoggle).

I recommend using registers instead of these options, but if they are still too scary, this can be a convenient workaround while you're perfecting your vim chops.

See :help clipboard for more detailed information.

vim copy command to clipboard / buffer

The windows clipboard can be accessed through the buffer +. So pasting your clipboard as an ex-command can be done with <C-R>+. If you want to copy your ex-commands to the clipboard, you need to show the command history (q:) and copy it into the clipboard buffer ("+yy).

vi/vim editor, copy a block (not usual action)

Another option which may be easier to remember would be to place marks on the two lines with ma and mb, then run :'a,'byank.

Many different ways to accomplish this task, just offering another.

vim - process the output from the read command as a range in Ex mode

Vim sets the change marks '[ and '] to the inserted range; you can use these to define a range for subsequent Ex commands:

:cd . | execute 'r ! dir /w/s/b' | '[,']s/^/    /

You need :execute because otherwise the | is interpreted to belong to the :r command.

How to copy to clipboard in Vim?

The * register will do this. In Windows, + and * are equivalent. In unix there is a subtle difference between + and *:

Under Windows, the * and + registers
are equivalent. For X11 systems,
though, they differ. For X11 systems,
* is the selection, and + is the cut buffer (like clipboard).
http://vim.wikia.com/wiki/Accessing_the_system_clipboard

* is probably what you want most of the time, so I use * because it functions as I expect it to in both environments.

In Linux distros you have to install vim-gtk (aka gvim) first to gain clipboard functionality. This is because non-gtk vim is typically compiled without X11 support. This is to allow it to run on console only machines (often servers).

And for those confused about how to use registers when yanking or putting, you merely write " then the name of the register. So for copying something to the clipboard register you type "*y and then to put you type "*p (credit: Kyle Mathews)

How to copy text from command-line mode in Vim?

The fastest way is to run the command, switch to the destination
buffer (with .vimrc loaded, in this case) and paste the whole
command from the : register by typing

":p

in Normal mode.

If the command is further back in time, one can first recall it from
history (e.g., by typing the first few letters and pressing the up
arrow key ), rerun it, and then use the above method.

When these shortcuts are unhandy, one can resort to the general
approach of using the command-line window (see :help cmdwin).
To open it, either type q: in Normal mode, or press the key
combination set by the cedit option (Ctrl+F,
by default) in Command-line mode.



Related Topics



Leave a reply



Submit