How to copy text into Vim


In this article you are will learn how to paste code or text inside the Vim editor from an external file.

The instructions below work on the popular Putty client for Windows. Look further down for more generic instructions, including how to copy code into Vim without changing its formatting (important for Python code).

Windows and Putty

We assume that you have used the Putty software to connect remotely to your Raspberry Pi.

A good starting point is the relevant documentation from Putty for future usages, so check section 3.1.1 Copying and pasting text.

You can follow these two methods:

Method 1. Shift + Ins

First, please make sure to turn off the Num Lock in order your Ins button to work as expected

  1. copy the content of the text from the text file (Ctrl-C or right-click & copy)
  2. open the file you want to edit with the vim editor
  3. type ‘i’ to enter the insert mode ( check at the bottom for — INSERT –)
  4. hit this key combination: Shift + Ins

Method 2. Shift + Right-Click & Paste

  1. copy the content of the text from the text file (Ctrl-C or right-click & copy)
  2. open the file you want to edit with the vim editor
  3. type ‘i’ to enter the insert mode ( check at the bottom for — INSERT –)
  4. hit this key combination: Shift + Right-click & choose the ‘Paste’ from the menu

For any operating system and client

Regardless of which operating system or client you use to connect to a remote host and use Vim, you can use regular copy/paste to copy text while in a Vim session. Follow this process:

  1. Use Vim to open the file that you want to edit.
  2. Copy some text by highlighting it with your mouse and typing Cmd-C or Ctr-C.
  3. In Vim, go into insert mode by typing “i” (notice that “– INSERT –” appears on the bottom of the Vim session).
  4. Type Cmd-V or Ctr-V to paste the text.

If you are copying text for which the formatting should not change, beware that the above process can introduce changes to the formatting. For example, if you are copying Python code into a file using Vim, be prepared for the indentation to be altered, and therefore your program to not work as expected. In Python, code blocks like loops are denoted using text indentation.

To avoid this from happening, you can use Vim’s paste mode. When you enable paste mode, Vim will not auto-indent any text that you paste. To enable paste mode, follow this process:

  1. In Vim, ensure you are command mode by hitting the Esc key.
  2. Type “:set paste” to enter command mode.
  3. Type “i” to enter paste mode. Notice the “– INSERT (paste) –” at the bottom of the Vim window.
  4. Paste your Python code into Vim. Indentation should be as in the original
  5. To exit paste mode, type “:set nopaste”. Notice the “– INSERT –” at the bottom of the Vim window. This means you are back to normal insert mode.

Resources: