How to copy paste contents in the vi editor

First, make sure you're in edit mode (press i). Then you can paste with Ctrl+Shift+V, if you're in a terminal emulator like gnome-terminal (or select "Paste" from the right-click menu).

You can also type :set paste in vim before you paste to disable automated indenting, etc. Then :set nopaste after you've pasted the content.

Also check this question on stackoverflow.com for more information.

If you want to copy/paste lines in vim (as opposed to pasting clipboard content), you'll want to check out the yank command. Here is a cheat sheet that might help.


Vi (and Vim) works very differently compared to a normal text editor such as Gedit. It also has a pretty steep learning curve. If you want to learn some basic commands, start with this interactive tutorial.

However, to answer you question. The system clipboard's content can be accessed through the plus register. So to paste something from the system clipboard you can, from the Normal mode, press: "+p (Not at the same time, but one after another).


  1. If you want to copy paste contents within the same file, use yank and paste.

  2. If you want to copy paste contents across terminals, open the first file, yanking the text you want, then open your second file within vim (e.g. :tabnew /path/to/second/file) and press p to paste it.

  3. If you want to copy paste contents from vim to an external program, you need to access the system clipboard. I assume you use Ubuntu. The GUI version of vim always has clipboard support, however, if you like to use Vim from a terminal, you will have to check for X11-clipboard support.

    From the console, type:

    $ vim --version | grep xterm
    

    If you find -xterm_clipboard, you have two options:

    1) Compile vim yourself, with the xterm_clipboard flag on

    2) Uninstall vim, install gvim (vim-gtk or vim-gnome) instead. You can stick to non-gui vim by calling vim from the terminal, the same way you did before. This time when you check you should find +xterm_clipborad.

    Now, when you yank some text in the + register inside your vim editor (e.g. "+yy), it also gets copied to the system clipboard which you can retrieve from your external program like gedit editor, by using Ctrl+V.

  4. If you want to copy paste contents from an external program into vim, first copy your text into system clipboard via Ctrl+C, then in vim editor insert mode, click the mouse middle button (usually the wheel) or press Ctrl+Shift+V to paste.

    These are 4 basic copy & paste conditions related to vim. I hope this helps.

Tags:

Vi

Editor