34 C
Jaipur
Tuesday, October 20, 2020

How to Copy Text to Clipboard in Vim

Must read

Ahmedabad Top Indian City for Mobile Gaming, Suggests OpenSignal Study

Mobile analytics platform OpenSignal recently conducted an analysis of the network experience across 48 Indian cities when it comes to gaming. OpenSignal ranked these...

Chrome Allows Google, YouTube to Bypass Settings and Keep Tracking Users

An alleged bug in Google’s Chrome web-browser apparently allows the US tech giant to continue tracking its users even after it has been explicitly...

How to Remove Data Plan from Apple Watch

If you have had a cellular Apple Watch, chances are you already have it set up with your data plan. However, if, like me,...

In this article, I am going to show you how to copy text from Vim editor to another program. Our goal is to get the content copied to the clipboard.

To use copy and paste within the vim is easier using yank command or using registers. But when it comes to copy to another application say to a browser we need extra steps. Here I will explain two methods to copy text to your system clipboard.

  • By Installing vim-gtk
  • By Compiling Vim

Method 1: By Installing vim-gtk

So, how do we copy from the Vim to another Vim window or a browser? By default, Vim does not have this ability to access your system clipboard, but getting this ability is actually extremely simple. One way is just to compile a version of Vim that has access to clipboard, another lazy way is to just install gvim (also known as vim-gtk).

Gvim is a graphical version of Vim. When you install gvim, it will give the normal terminal vim its copying and pasting abilities to your clipboard. In other words, it gives you the ability to copy to a special register that corresponds to your system’s clipboard. Special register is a +. It is really just a register you can copy things to and that will automatically put it in your system clipboard.

To install gvim (or vim-gtk), run:

$ sudo apt-get install vim-gtk

Let us consider an example of copying a link address and paste it to the browser. Please open our example file and navigate to the last line. Now, you have to run the following command:

V"+y

where:
+  is your system’s clipboard

Open your browser and press ctrl+v the same way you would normally paste and see that now it is pasted in. We can go to that site and get something from it, copy and paste to our Vim. Let’s copy the same way that we do normally with ctrl+c. Now, we can access this copied line in Vim by running the following commands:

"+p

You see that it successfully pastes the content from the browser to Vim.

Method 2: By Compiling Vim

Another way is to compile Vim to support clipboard copying. The default Vim does not have it under the hood. So, to check, whether this option is valid, please run:

vim --version | grep .xterm_clipboard -o

If you’ll get something like this: -xterm_clipboard

This means that copying to clipboard is not supported and you have to compile your Vim. Worth to mention, that only the Vim version with GUI supports clipboard access. So, in order to recompile the Vim you have to start by getting the compile dependencies of Vim and install mercurial:

$ sudo apt-get build-dep vim
$ sudo apt-get install mercurial

Now, get the source:

$ sudo hg clone http://hg.256bit.org/vim

If you already had this repository clone, then update it:

$ sudo hg pull
$ sudo hg update

And then check if you have the latest version:

$ hg summary

Now, you have to compile it:

cd vim_source
./configure 
   --enable-perlinterp=dynamic 
   --enable-pythoninterp=dynamic 
   --enable-rubyinterp=dynamic 
   --enable-cscope 
   --enable-gui=auto 
   --enable-gtk2-check 
   --enable-gnome-check 
   --with-features=huge 
   --with-x 
   --with-compiledby="YourName <[email protected]>" 
   --with-python-config-dir=/usr/lib/python2.7/config
make && sudo make install

That will install it in /usr/local path. Please note that you will end up with two vim’s: /usr/bin/vim without access to your clipboard:

vim installed in /usr/bin/vim

vim installed in /usr/bin/vim

and /usr/local/bin/vim with access to your clipboard:

vim installed in /usr/local/bin/

vim installed in /usr/local/bin/

And with gvim /usr/local/bin/gvim (with a symlink to /usr/local/bin/vim. It will support clipboards whether or not you’re in GUI mode. And you can always run the old one with the explicit path.

Once everything is compiled, you can use /usr/local/bin/vim to open any file and copy-paste text the same as we considered before by using the ‘+’ register.

How to store text into register

Registers are small storage space in on your system that can store the contents you have yanked or copied. You can use many registers to store different yanked contents.

Use the following command  will select a line and store to ‘a’ register:

V"ay

where:

V (capital V) – for marking the whole line
” (double quote) – to access the register
a – register name
y – yank (copy)

And for the ‘b’ register

V”by

Now, we have saved text in two different registers and we can paste them out, and we can paste them out in a different order. For instance, we can paste out ‘b’ register by running the following command:

“bp

where:

p – paste

Conclusion

In this article, we have considered two ways of how to enable access to your clipboard in Vim. Both options are valid and it seems that installing the vim-gtk package is faster and efficient than compiling Vim.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Ahmedabad Top Indian City for Mobile Gaming, Suggests OpenSignal Study

Mobile analytics platform OpenSignal recently conducted an analysis of the network experience across 48 Indian cities when it comes to gaming. OpenSignal ranked these...

Chrome Allows Google, YouTube to Bypass Settings and Keep Tracking Users

An alleged bug in Google’s Chrome web-browser apparently allows the US tech giant to continue tracking its users even after it has been explicitly...

How to Remove Data Plan from Apple Watch

If you have had a cellular Apple Watch, chances are you already have it set up with your data plan. However, if, like me,...

Mi Watch Color Sports Edition with Blood Oxygen Monitoring Launched in China

Xiaomi unveiled the Mi Watch Color in China earlier this year. It has since found its way to India as the Mi Watch Revolve....