Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Pasting in command line Vim doesn't have to suck (userobsessed.net)
29 points by joefiorini on May 10, 2011 | hide | past | favorite | 31 comments


Vim can actually access the system clipboard (and, on Linux, the X clipboard). It's accessed as the * register (or + for the X clipboard). So, for instance:

    "*dd
    "*p
or

    "+yy
    "+P
And so on. Very convenient (and also much faster than Ctrl+V). On Ubuntu, this is only available in the Vim provided by vim-gnome or vim-gtk (even when running in a terminal).


It's only available when Vim is compiled with +xclipboard support. I actually found that on an older version of CentOS (v4, I don't know if it's different in newer versions), the 'vim' command and the 'gvim' command were separately compiled binaries so that 'vim' command didn't share the +xclipboard support. On Debian/Ubuntu, I believe they are the same binary.


Exactly. On my machine (Ubuntu 11.04) the binary (/usr/bin/vim.basic) provided by the vim package is compiled with -clipboard -xterm_clipboard, and the binary (/usr/bin/vim.gnome) provided by vim-gnome is compiled with +clipboard +xterm_clipboard.


Honest question: how is that faster than Ctrl+V? Is it because you don't need the mouse?


I'm sorry for the confusion. There are two ways it can be faster. The way you understood it, I guess, is in typing. The difference here is:

  i Ctrl v Esc
vs

  (Shift) " * p
Where, on my keyboard, both " and * requires the shift key, but YMMV.

What I meant, though, is the actual input of text. When pasting with Ctrl+V, that's like one character at a time for Vim, and that's visible, so if you paste a large body of text you can actually see it scrolling past. When working with buffer commands, it works just any other internal Vim buffer operation, which is instantaneous.


Its not. You usually do most of your copying and pasting within vim so you can just use the normal past command, but it is annoying when you want to do something with the system clipboard.


Vim has a built-in directive to toggle paste mode:

set pastetoggle=<F3>

http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_pas...


Another method I sometimes use for pasting large blocks of code:

    :r !cat
    (paste your stuff)
    ctrl - D
Doesn't require any switching between paste modes.


Nice idea, and you can replace cat if you need to clean up the paste:

    :r!perltidy
    (paste coworker's ugly perl code)
    ctrl-D
and you have nice looking code pasted.


Interesting.... I see how it works, any other benefit to this approach?


When the author says "command line Vim" he means "Vim running in a terminal emulator".

And pasting to terminal Vim doesn't suck if your Vim is configured to detect mouse clicks or interface with the system clipboard. Using the system paste shortcut (command- or ctrl-V) in insert mode is not the way to do it.


Unless, of course, the system clipboard is on a different machine than the running copy of Vim. This is useful advice, you really don't have to be so negative about it just because you aren't in a situation where it's applicable.


If your ssh session has X11 forwarding enabled, and the remote terminal Vim has +xclipboard support, then you can use the "+P keystroke to paste directly from the clipboard into Vim.


Good point on my wording, thank you. I will change that to make it less confusing. As for middle-click, I really don't want to use the mouse. I don't use it for pasting anywhere else, so why would I change that for Vim?


  :set pastetoggle=<F5>
Wack F5, middle-click paste away.


Sure, that works great if you want to reach for the mouse.


As mentioned elsewhere, if your vim is compiled with clipboard support(:version and look for +clipboard), you can copy/paste from the clipboard.

* is the Linux selection register.

+ is your clipboard.

So yanking is just set paste, "+y or " * y, set nopaste. Similarly you can do a "+d in vim and ctrl-v it in your browser or anywhere you can access the clipboard. You can " * d/" * y and paste it with your middle mouse button.


The mouse has nothing to do with it...

If you paste method doesn't let vim know what's going on, use pastetoggle, otherwise don't.


Pretty sure vi does not care about, or is even able to detect, the difference between a middle-mouse and a shift-insert paste.


Unless your terminal emulator has mouse support and you ':set mouse=a' in Vim.


Or you can use shift+insert.


Although if you're using Vim over SSH from a Windows box, the :set paste command is useful to know.


perhaps, I'm forgetting something, but these solutions seem overly complicated.

I just have this in my .vimrc, and pasting (with formatting), is as simple as copying text, going to vim and hitting 'p' in command mode.

  for middle-click (system) clipboard:
    :set clipboard=unnamed
  for ctrl-c (X11) clipboard:
    :set clipboard=unnamedplus

  couple caveats:
  * +xterm_clipboard
  * 'unnamedplus' is only for >= 7.3.74 
see here for more info http://vim.wikia.com/wiki/Accessing_the_system_clipboard

note: I used unnamedplus for a bit, but found it to be much less desirable than using the unnamed middle-click buffer...YMMV


On a mac, you can use :r !pbpaste

If you're ssh'd somewhere it's a different story. I've seen some things about sharing clipboards across shells, but I've never bothered to set it up.

Also, copy-pasta makes for bad code.


Thanks for the tip! I also learned you can do:

:a! <paste like normal> <C-c>

and that will paste with properly formatted text.


... and I thought it was how to paste in vim commandline after :


How to paste in vim commandline after :

    <ESC>
    (normal mode)
    q:
    (fully vim editable window of :command line history)
    (edit one of the command lines, yank and put all you like)
    (or edit the new blank command line at the bottom)
    <ESC><ENTER>
    (to execute the current line, or)
    :q
    (to quit without executing)
    (normal mode)
Notice the symmetry of q: to get in, and :q to get out (if you don't execute).

q/ and q? to edit and execute search history.

[edit formatting]


ctrl-r <register> pastes that register into your command.

So I often need to find "where is this method defined" across a large project.

    yw (yank word to get method into the " register)
    :Ack <ctrl-r>"  (ack plugin, paste that default " register)
    <enter> yay! I found it.
Alternately:

    /foo  (attempt to find foo in this file. Whoops, not there, where is it!).
    :Ack <ctrl-r>/  (last search is in the / register).
    There it is!


wow! this is exactly what i was looking for. thank you.


"Argh, how do I paste?!"

  :h paste


You can also paste in "ex" mode.

ESC

Q --> to go to "ex" mode

i --> to start inserting

paste text here

. --> line containing a single period to exit "ex" insert mode

:vi --> to return to "vi" mode

...golfer, moi?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: