How to make vim paste from (and copy to) system's clipboard?

Viewed 847927

Unlike other editors, vim stores copied text in its own clipboard. So, it's very hard for me to copy some text from a webpage and paste it into the current working file. It so happens I have to either open gedit or type it manually.

Can I make vim paste from and to the system's clipboard?

35 Answers

I believe that this question deserves a more objective and pictorial answer:

Entering Paste Mode

  • ESC
  • :set paste
  • press i
  • SHIFT + Insert (with a text copied on your clipboard)

**Leaving Paste Mode**
  • ESC
  • :set nopaste
  • press i

You pasted the text and you're able to type again.

Copy To OS Clipboard

Select text in visual mode, press "*y

Paste From OS Clipboard

Press "*p

I ran into this issue on a mid-2017 Macbook Pro running vim within iTerm2 as my primary development environment.

As other answers have suggested, I ran vim --version and noticed that it returns -clipboard, which means that the version of vim that shipped with my machine hasn't been compiled with the clipboard option.

The homebrew package for vim appears to compile with the clipboard option, so the fix for me was to:

  1. Run brew install vim
  2. Add set clipboard+=unnamed to my ~/.vimrc file
  3. Close and reopen iTerm2

What simply worked for me in Linux (Ubuntu 20.04)

  • Copying to system clipboard:

    1. Select what you want to copy in Visual mode.
    2. Type "+y
    3. Press Enter
  • Paste something form system's clipboard:

    1. Move to the place where you want to paste the copied text in vim.
    2. Just type
      • "+P to paste before cursor OR
      • "+p to paste after cursor.

To know more how this works: Copy and Paste to/from Vim from/to Other Programs!

These key combinations work on any OS.

Select target Text using the mouse, and refer to the key sequences to copy, cut, and paste.

  • copy: Ctrl + Insert
  • paste: Shift + Insert
  • cut: Shift + Del
  • paste: Shift + Insert

Shift + Right Click -> Paste

did the trick for me

With Vim 8+ on Linux or Mac, you can now simply use the OS' native paste (ctrl+shift+V on Linux, cmd+V on Mac). Do not press i for Insert Mode.

It will paste the contents of your OS clipboard, preserving the spaces and tabs without adding autoindenting. It's equivalent to the old :set paste, i, ctrl+shift+V, esc, :set nopaste method.

You don't even need the +clipboard or +xterm_clipboard vim features installed anymore. This feature is called "bracketed paste". For more details, see Turning off auto indent when pasting text into vim

If you have it, try removing this from your vimrc: set mouse=a

It messes with the paste functionality.

On top of the setting :set clipboard=unnamed, you should use mvim -v which you can get with brew install macvim if you're using vim on Terminal.app on Mac OS X 10.9. Default vim does not support clipboard option.

Since vim 8 right click enables visual mode by default. This prevents the "normal" copy & paste (call it a "defect by design" https://github.com/vim/vim/issues/1326). Fix it by doing:

echo "set mouse-=a" >> ~/.vimrc .

Exit and restart vim.

After entering the vim window, press I to enter into insert mode. Then move your cursor to the desire location and press Ctrl + Insert button simultaneously to paste from the clipboard.

I used the answer of NM Pennypacker and installed vim via homebrew for an early 2011 MacBook Pro:

brew install vim

Now I can also use the "* register to copy and paste text within vim. I even didn't have to change something within my ~/.vimrc file or the $PATH. Homebrew added symlinks in /usr/local/bin, e.g. vim -> ../Cellar/vim/8.1.2350/bin/vim.

The alternative, which worked before, is to copy some lines of text within vim by marking it with the mouse and using copy and paste (cmd + c, cmd + v) on a mac. This option only works if the text you want to copy and paste is less in size than the window size of vim. If you want to copy all text within vim by marking the whole window or using cmd + a, this will copy other parts of the console, written before starting vim, which is very annoying.

So, I am happy having found the new method using the clippboard register.

Mac OS X:

  1. run vim --version | grep clipboard to check if the clipboard is enabled(with +)
  2. add set clipboard=unnamed to .vimrc
  3. restart terminal & vim
Related