How to Paste Text in Vim (from the Clipboard)

Paste Text into Vim

by | Apr 14, 2024 | Useful Tech

Looking to include a URI you copied from your browser or text from a… text? Find out here how to paste text in Vim and Neovim, both from within the editor and from the system clipboard using Windows, Mac and Linux.

First, Time is a Flower

If a clock is an idealized representation of planetary motion and the planets’ idealized motions can be represented with flowery circles, how about a clock that uses circles inside circles to show the passing of time?

With the second hand attached to the minute hand attached to the hour hand, Sophie Houlden’s epicycle clock turns time into a blooming flower.

Now, with circles inserted into circles and time into itself, how about inserting text into Vim (into registers)?

How to Paste Text into Vim from the System Clipboard

Using the Clipboard Register

Time needed: 1 minute

To paste text from the system clipboard into Vim (or Neovim) on Windows, Mac and Linux:

  1. Position the cursor where you want to insert the pasted text in Vim.

    What you see is what you paste: See below for getting a preview.

  2. Switch to normal mode.

    Here’s how: Press Esc; typically to exit insert mode in Vim, Vim and Neovim will either display normal mode or nothing in the status line, depending on how the line is set up.
    Just for pasting: Press Ctrl O in insert mode to switch to normal mode only for the next command, and you will effectively stay in insert mode.

  3. Type "*.

    Here’s why: This selects the special register * (star), which corresponds to the system clipboard.
    No star register: Note that the system clipboard is only available if Vim has been compiled with support for it. You can check with the :version command in normal mode, looking for +clipboard as an option.
    Windows alternative: On Windows, you can also use "+ for the + register, which also corresponds to the system clipboard.

  4. Now press P to paste text from the clipboard after the letter that currently has the input focus.

    Before the cursor: Press Shift P instead to insert the pasted text before the current cursor position.
    Moving the cursor: You can also move the cursor right after the pasted text; for this, prepend P or Shift P with G.
    Example: "*gP will paste text from the system clipboard after the cursor and move the cursor to the spot right after the pasted text.
    Use "*P in normal mode to paste from the system clipboard in Vim

Using Paste Mode or Vim with a Graphical User Interface

To paste text into Vim or Neovim from the system clipboard using “Insert (paste)” mode:

  1. Enable Paste mode in Vim or Neovim on the command line.
    Here’s how: Press Esc for normal mode, type :set paste and press Enter.
    Graphical Vim: In Vim with a graphical user interface (not in a terminal or terminal application), you need not turn on Paste mode; pasting in Insert mode will work just fine using the following steps.
  2. Now navigate to the position where you want to insert text.
  3. Switch to Insert (paste) mode.
    Here’s how: Press I, for instance, to start inserting before the current character, A to start after the current character, or O to insert a new line after the current one.
  4. Use the terminal application or app to paste from the system clipboard.
    Right-clicking: Often, clicking with the right mouse button will bring up a context menu that includes a Paste command.
    Menu: The terminal app will also typically have a menu that includes choices for pasting.
  5. Press Esc for normal mode.
  6. Now disable Paste mode again.
    Here’s how: Type :set nopaste and press Enter.

Paste Text from within Vim

To paste text you have yanked (copied) in Vim itself:

  • In normal mode, press P to paste after the text cursor and Shift P to paste before the current character.
    Going with the flow: To move the cursor just after the pasted text, prepend the commands with G; gP will paste after the cursor and move the text cursor after the paste text, for instance.
    Custom clipboards: Vim and Neovim support multiple clipboards by way of named registers, of course. To paste from a named register, type " followed by the register’s name before the pasting command.
    Example: Type "ap in normal mode to paste from the a register in Vim.

View the Contents of All Vim Registers (Clipboards)

To preview what pasting any register would paste:

  1. Switch to normal mode in Vim.
    Here’s how: Most typically, press Esc.
  2. Type :registers for a list of all current registers (clipboards) together with their contents.
    Just one: Use :register <name> instead to see just the register named <name>; see below for a list of possible registers.
    Example: Type :register * to see the system clipboard register and get a preview of what will be pasted from it.
  3. Press Enter.

Important Vim Registers

Vim register nameContents
* or +System clipboard (under X11, * refers to the primary and + to the clipboard selection)
Most recently yanked (copied) or deleted text (default when no register is given)
0Most recently yanked (copied) text
1Most recently deleted or changed text
2–9Previously deleted or changed text (moved up with each successive action)
a–zRegisters only accessed manually (using "a through "z)
:Most recently typed command-line command (starting with :)
.Most recently inserted text
/Most recent search pattern

How to Paste Text into Vim (from the System Clipboard): FAQ

My Vim does not include clipboard support; can I still paste from the system clipboard?

Yes.

Even without support for the system clipboard, you can paste text into any document with Vim.

To paste from the system clipboard using a command-line utility:

  1. In Vim, position the text cursor where you want to insert text pasted from the clipboard.
    New line: Note that the clipboard text will typically be pasted as a new line after or before the current one; it is best to temporarily break the line where you want the text inserted, then join lines again.
  2. In normal mode, type "=.
    Here’s why: This selects the command register to hold the output for pasting.
  3. Now type system('<command>') and replace <command> with the following depending on your operating system:
    Windows: Use powershell get-clipboard (this assumes Windows PowerShell is installed).
    macOS: Use pbpaste.
    Linux: Use xclip -o -sel clip (this assumes Xclip is installed).
    Example: On Windows, the command line will read =system('powershell get-clipboard').
  4. Press Enter.
  5. Now press P to paste after the current line and Shift P to insert before it.
    No new line: You can in theory strip the newline character from the register before pasting so it is inserted inline in text; for all practical purposes, this is too cumbersome, however.
    Many paths: The method described here is but one of many that let you insert from the clipboard; they all accomplish the same.
  6. Optional: Use J to join lines.

(How to paste text into Vim tested with Vim 9.0–9.1 and Neovim 0.9.5; updated April 2024)

Home » Useful Tech » How to Paste Text in Vim (from the Clipboard)