Vi – Copy Current Line to Clipboard Under Cursor Without Mouse Selection

clipboardvi

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.

Best Answer

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
Related Question