Vim: Cut, Copy and Paste to and from the system clipboard

Vim: Cut, Copy and Paste to and from the system clipboard

vim_logo.png

Figure 1: Vim

1 Cut, Copy and Paste using the computer keyboard

Everybody knows how to use the keyboard combinations Ctrl-c, Ctrl-x, and Ctrl-v in Windows and Linux (or Cmd-c, Cmd-x and Cmd-v in macOS) to copy, cut and paste text across applications.

Many think that those combinations are universal and work with any application.

But Vim and gVim are different, they use another system to exchange information with the rest of the IT world.

For example, in Vim under Linux, the key combination Ctrl-v corresponds to the visual block function and does not affect the pasting text.

I wrote this article to remind me, as a non-programmer, the key combinations to be used in the Vim world to share text with other applications.

2 Vim and the clipboard

Vim uses an original resource to exchange text with other applications.

If you press the Vim (or gVim) :reg command, you can notice a list of items beginning with a double quotation mark (") and followed by a symbol, a letter or a number: these are the registers.

Vim uses a specific register to interact with the system clipboard and, consequently, with other applications.

This specific clipboard register is identified with the mathematical plus symbol (+) 1

3 From Vim to the Clipboard

If you need to transfer information from Vim to other environments you must use the key combination ​"+y to copy the text and ​"+x to cut it from the original document.

As shown in the official menu visible in gVim:

cutocopyconverted.png

Figure 2: Cut vs Copy

Pay attention to the following table:

Key What is?
" The registers symbol
+ The clipboard register symbol
x The letter to cut the text
y The letter to copy the text

Then you have to press three keys in sequence:

  1. ​"​
  2. +
  3. x or y

In both cases, the cut or copied text is sent also to the clipboard and can be pasted outside Vim using the usual Ctrl-v command, in Windows and Linux, or Cmd-v in macOS.

4 From the Clipboard to Vim

The reverse operation is also quite simple. You can transfer your text copied from the clipboard using the string ​"+gP.

pastevimconverted.png

Figure 3: Paste text string

Let's analyze every single component of the string:

"P" means paste before the current position.

The "g" key puts the cursor after the pasted text.

Then "gP" command produces the following result: Paste before the current position, placing the cursor after the pasted text.

If you don't use g the cursor will be placed on the last letter of the pasted text and you'll waste time to advance the cursor by one character.

In Vim philosophy everything must be optimized, also the single movements of the cursor!

5 Some key shortcuts in gVim

In some cases, it is also possible to use key shortcuts in gVim.

They mainly work in MS Windows but some of the work also in several Linux distributions. I can directly confirm the working in Mint, Fedora, Ubuntu, Antergos for the pasting shortcut (see list below) but not the same for the others.

The following is the list of shortcuts:

Shortcut What happens?
Shift+Delete Cut text and copy it in the clipboard
Ctrl+Insert Copy text in the clipboard
Shift+Insert Paste text from the clipboard into a document

Thank You for Your attention.

Footnotes:

1

There's also another symbol for the clipboard: the asterisk (*): it produces almost the same results of the plus symbol but in GNU/Linux OS it saves information specifically for the middle mouse button.

Author: Franco Pasut

Created: 2019-08-22 gio 15:45

Comments

Post a Comment

Popular posts from this blog

Vim: searching for text containing a slash or a question mark