7

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • `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 08 '15 at 18:31
  • @Madhavan Kumar: What is your distro? – Mohammad Etemaddar Jul 08 '15 at 18:50
  • What do you mean simpler? Do you mean `dd` is simple so? – Mohammad Etemaddar Jul 08 '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 08 '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` –  Jul 08 '15 at 23:02

1 Answers1

8

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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • thanks a lot, Gilles.. `clipboard` feautre was not enabled in my vim, so i used your suggestion#2 and it works... –  Jul 09 '15 at 02:29
  • note: installing `gvim` gave me the clipboard feature – Jacko Dec 08 '18 at 21:36
  • Of course, what `vi` (per the question) actually maps to is important. VIM is as this answer. NeoVIM, however, implements the `*` and `+` registers using the external utilities. (See its `:help clipboard`.) – JdeBP Aug 14 '20 at 06:30