Vim and Emacs: Cut, Copy and Paste

emacs-vim.png

Figure 1: Vim and Emacs

1 The traditional Cut, Copy and Paste key combinations

Ctrl-c, Ctrl-v and Ctrl-p are among the most common and traditional key combinations in the world of computers.
Everybody knows how to use the key combinations Ctrl-c, Ctrl-v and Ctrl-p (in Windows and Linux) or Cmd-c, Cmd-v and Cmd-p (in Mac Os) to interact with the operating system clipboard. You might think that the above combinations are valid for every situations and then that every software uses the same method to copy and paste information.

But that's not true.

In this article we see how the same operations take on different forms in two famous writing environments: Vim and Emacs.

2 Vim and the clipboard

Vim doesn't use the Ctrl-c, Ctrl-v and Ctrl-p commands to interact with the system clipboard.
It has its own memory management system separate from the operating system one and uses the registers.

If you display the register status, by using the :reg command, you can notice a list of items beginning with a double quote or quotation mark (​"​) and followed by a symbol, a letter or a number: these are the registers.
The quotation mark is the character identifier for the registers.

Vim uses a specific register to interact with the system clipboard.

This specific register is identified with the mathematical plus symbol (+).
In Vim there's also another symbol for the clipboard: the asterisk (*) one. The asterisk symbol produces the same results of the plus symbol except in GNU/Linux OS where it saves information specifically for the middle mouse button.
We can then refer only to the plus symbol.

3 From Vim to Clipboard

It's easy to transfer a fragment of text from Vim to the clipboard and vice versa.

The first step is to collect the informations from within Vim and to store them in the clipoard register.
To achieve this result you must use the key combination ​"+y if you want to copy the text and ​"+x in you want do cut it.

cutocopyconverted.png

Figure 2: Cut vs Copy

Analyze each individual character of the above combinations:

  1. The ​"​ character is general identifier for a register.
  2. The + character is the specific identifier for clipboard register.
    1. The x character is the command to cut text.
    2. The y character is the command to copy text.

Then you must type three characters in succession:

  1. The firs charater is: ​"​
  2. The second character is: *
  3. The third character is: 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 Ctrl-p command.

4 From Clipboard to Vim

The specular operation is also quite simple. You can transfer your text copied from the clipboard via the command ​"+gP.

pastevimconverted.png

Figure 3: Paste text string

You can notice interesting letters in this command.

"P" means paste before the current position.

The "g" letter 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 real Vim everything must be optimized, also the single movements of the cursor!

5 Vim and the shortcuts

Vim provides an alternative method for copying and pasting text: the/shortcuts/.

The shortcut method is very simple to use but doesn't work in every Vim installation.

It works in MS Windows and in some Linux distributions: I tried with success in Mint, Fedora, Ubuntu, Antergos (if you find any other positive feedback please let me know).

The following one is the list of shortcuts:

  • Shift+Delete to cut text in the clipboard
  • Ctrl+Insert to copy in the clipboard
  • Shift+Insert to paste text from the clipboard

You can notice that the insert key is used to copy and to paste and that the shift key is used to cut/and to /paste. The Ctrl is used only to copy.

To remember the combinations you can think of the Ctrl key as a Copy key and the Delete key to cut. Insert is dedicated to copy and paste.

Here's a simple mind mapping to remember the above shortcuts:

Copy+Cut+Paste+Vim+%281%29.png

Figure 4: Shortcuts in Vim

6 Emacs and the clipboard

Emacs makes a very easy task to transfer text to and from the clipboard.

In Emacs you do not cut the text but you kill it! At the same time you do not paste text but you yank it!

Then in Emacs the action to paste is called to yank and the action to cut is called to kill.

In Emacs the copied or cut text is sent directly in the so-called kill ring and is also immediately available in the system clipboard.

The cut or copy clipboard operations performed in other applications in the kill ring.

The kill ring is a continuous programme schedule containing the gradually killed text. The last killed fragment of text is the top one in the ring and it's directly available to be yanked inside Emacs or to be pasted in the clipboard using the traditional Ctrl-p key combination mentioned at the beginning of this article.

7 Kill, Copy and Yank in Emacs

cutcopyemacsconverted.png

Figure 5: Kill and Yank

The easiest way to yank (copy) text in Emacs is to use the command M-x in a region. The easiest way to kill (cut) text in Emacs is to use the command C-x in a region.

The opposite operation is as easy as the previous one.

pasteemacsconverted.png

Figure 6: Paste text

If you use the universal shortcuts Ctrl-x or Ctrl-c in an external editor you can directly yank (paste) your text (not registered in the kill ring inside Emacs using C-y.

This article itself is written in Emacs Org Mode.

Thank you for your attention.

Comments

Popular posts from this blog

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

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