VIM and clipboard

VI improved (VIM) has very nice mouse support which in most Linux distributions activated by default or can be activated by following command.

set mouse=a

But when the mouse support is enabled by default in VIM, it becomes harder to copy a text by selecting within VIM and copy it to system’s clipboard. The way I used to do it was, 1st disable the mouse mode temporarily by running “:set mouse=”, and then select the text which I want to copy using mouse. This mouse selection will now use the terminal’s “test selection” implementation and will allow the terminal’s capability to copy the selected text. This method works most of the times, but when copying the source code from one file to another (which is open in a different vim instance) will cause problems. The default text selection behavior of terminal also includes the white spaces at the end of lines in vim, even though there are no spaces at the end of lines.

The ideal way would be to have VIM can access the system clipboard, so that whatever copied from vim and be accessed anywhere outside vim. And vim developers might have heard the voice of lots of users, because this feature can be enabled in vim with clipboard support. Following command will show weather the currently installed vim version has the clipboard support or not.

vim --version | grep clip

If you can see “+xterm_clipboard and +clipboard” in the output, the vim version has the support for it. If no, there are additional packages which can be installed:
For Ubuntu, vim-gnome or vim-gtk can be installed. For Fedora, vim-X11 can be installed, which will in turn provide a new vim executable named “vimx” and can be aliased to “vi”. Once installed, following line can be added in the .vimrc to enable vim to access the system clipboard.

set clipboard=unnamedplus

Now if you copy the text from vim using “y” command, it can be access outside that vim instance, to anywhere in system.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s