Vim: Cut, Copy and Paste to and from the system clipboard
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 (+
)
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:
Then you have to press three keys in sequence:
- ”
- +
- 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
.
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:
Shift+Delete =
Cut text and copy it in the clipboardCtrl+Insert =
Copy text in the clipboardShift+Insert =
Paste text from the clipboard into a document
Thank You for Your attention.
Originally published at https://francopasut-en.blogspot.com on August 22, 2019.