Easy copy-pasting in Vim

My copy-paste related configuration for Vim.

One typical problem with Vim, is that once somebody enter a piece text, it’s not always trivial to get that back without saving it to a file and opening it with another tool. Many solutions can be found on various forums. They are all great, but I was looking for something more intuitive.

For example, a method is to directly copy to the system clipboard with “+y. Unfortunately, Vim have to be compiled with a special feature, which is not always the case. Even worse, copying this way requires a different command than yanking to the unnamed register (the default behavior), so additional shortcuts have to be memorized even for the simplest of tasks. There are many different scenarios where copy-pasting is required, and I would like to keep the trivial cases as intuitive as possible.

If Vim supports the system clipboard you can set it as the default with set clipboard=unnamedplus, but it can also be inconvenient. For example to change a piece of text inside parenthesis with something from a Stack Overflow comment, you can’t do the following:

  1. Copy the text from the browser.
  2. Navigate inside the parenthesis in Vim.
  3. Press ci( to go to Insert mode and clear the text.
  4. Press Shift+Ins to insert the new text.

After the third step the contents of the clipboard will be overwritten with the recently cleared old text, so the newly found solution is vanished.

In this post I’d like to share my configuration related to copy-pasting, that allows Vim to step aside and let the terminal handle the text directly.

Disable the Mouse

A typical copy-pasting task is to get a single line (or a few words) of code from Vim, and search for it online. Unfortunately, set mouse=a can prevent that, so the first thing to do, is to disable Vim’s mouse support. Although it’s not enabled by default, it’s common to use it. I enabled it as soon as I found this feature

  • it made me feel a bit safer when I started using Vim.

Nowadays, I aim to use the mouse as rarely as I can, so I dismissed it. Now I can use the terminal emulator’s facilities to copy text from the current page, just as I would do from any other command line application. This simple thing solves my most frequently arising problem and it requires no additional commands. As a bonus, I can use other features of the terminal too, such as opening links directly in the browser.

Tip: if you prefer to keep the mouse support, just press shift while selecting to temporarily disable Vim’s mouse-awareness and let the terminal handle the interaction.

Toggle sidebars

Copying multiple lines is tricky when you have a sidebar on either side of the text, so it’s useful to create key mappings that can toggle these things. I mapped the F11 key to toggle line numbers and the gitgutter.

silent! map <F11> :set invnumber <bar> :GitGutterToggle<CR>

(Multiple commands can be executed by separating them with <bar>.)

With every distractions removed, one can easily copy a whole screenful of stuff from Vim.

Open the whole file in the terminal

For the rare cases where I need to copy multiple pages, I created a mapping to pass the currently edited file to cat.

silent! map <F12> :!clear<CR>:exe '!cat %'<CR>

With F12 I can temporarily dismiss Vim, and view it simply in the terminal. Taking advantage of it’s scrolling, even multiple pages can be copied easily.


With this configuration copy-pasting is easy, and for the most part, it doesn’t require special key combinations to be memorized. Even moving large amounts of text is possible with a few additional mappings.

Happy Vimming!

24 January 2017

Interesting article?

Get hand-crafted emails on new content!