Tuesday, June 5, 2018

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


I'm a vim user and most of the times when I'm coding I use yy or Shift + y to copy a line of string from one place and p to paste in another place in the same file.
What if we wanted to copy a string from one file to another or from another application to vim. I had stuck in this problem sometimes ago and thought this would be a nice thing to mention in my BLOG even if it is a simple thing.

Unlike other editors, vim stores copied text in its own clipboard. So, it's very hard for me to copy some text from a webpage or terminal and paste it into the current working file.

If we want to copy text from one program to another we need to copy that particuar text to the system clipboard.

Most Linux and UNIX systems have two clipboards which are independent of each other.

1. PRIMARY - which usually copies things you select with the mouse and pastes with the middle mouse button.

2. CLIPBOARD - which usually copies things when you select and Ctrl+C and pastes with Ctrl+V

Vim has trow different registers for these clipboard

"* for PRIMARY and "+ for CLIPBOARD

On Windows & OS X there is no difference between "+ and "*, since these systems only have a single clipboard,

If We want to copy or paste from that register we needa to select needed register before the needed action yy or p.

If we want to copy to CLIPBORD from vim, use "*y after selecting the wanted text

If we want to paste from CLIPBORD to vim use "*p

Likewise,
If we Want copy to PRIMARY from vim , use "+y after selecting the wanted text

If we want to paste from PRIMARY to vim, use "+p

No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...