5

How can i copy the current line under cursor in vi to the system clipboard?

I may paste the contents anywhere, in vi itself, or in shell, or in libreoffice app... And i am looking for simpler shortcuts like, one does dd to delete the line.

  • yy will copy the current line to the clipboard, but I do not think it will copy it to the system clipboard to be used elsewhere. – airfishey Jul 8 '15 at 18:31
  • @Madhavan Kumar: What is your distro? – Mohammad Etemaddar Jul 8 '15 at 18:50
  • What do you mean simpler? Do you mean dd is simple so? – Mohammad Etemaddar Jul 8 '15 at 19:08
  • 1
    With Vim, provided that it's compiled with the right options, and provided that you're running it in an environment that support this: "*yy. – lcd047 Jul 8 '15 at 19:35
  • @MohammadEtemaddar, ubuntu 14.04 is my distro... yes, i see dd as easy to use as i don't need to move my fingers much when i am using this... basically, i am looking for copy command, and then paste it via ctrl + shift + v and not p – Madhavan Jul 8 '15 at 23:02
5

If your Vim is compiled with the +clipboard feature (check if +clipboard appears in :version or in vim --version), then there are two special registers that designate the system clipboard. The register "+ designates the clipboard, which is used by Ctrl+C/Ctrl+V. The register "* designates the primary selection, which is used by mouse selection and middle-click to paste.

To make a deletion, yank or put command act on an alternate register, prefix it with a double quote and the register character. For example, to copy the current line to the clipboard, type "+yy

If your Vim doesn't have the +clipboard feature, you can use an external utility to access the clipboard. You'll need Vim to have access to the X display, of course (the DISPLAY environment variable must be set). Use the :w command with an argument starting with ! to pipe the specified lines through a program. With xsel:

:.w !xsel -b
  • thanks a lot, Gilles.. clipboard feautre was not enabled in my vim, so i used your suggestion#2 and it works... – Madhavan Jul 9 '15 at 2:29
  • note: installing gvim gave me the clipboard feature – Jacko Dec 8 '18 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.