Paste in insert mode?

Vim

Vim Problem Overview


Is it possible to paste in insert mode in Vim?

Vim Solutions


Solution 1 - Vim

While in insert mode hit CTRL-R {register}

Examples:

  • CTRL-R * will insert in the contents of the clipboard
  • CTRL-R " (the unnamed register) inserts the last delete or yank.

To find this in vim's help type :h i_ctrl-r

Solution 2 - Vim

If you don't want Vim to mangle formatting in incoming pasted text, you might also want to consider using: :set paste. This will prevent Vim from re-tabbing your code. When done pasting, :set nopaste will return to the normal behavior.

It's also possible to toggle the mode with a single key, by adding something like set pastetoggle=<F2> to your .vimrc. More details on toggling auto-indent are here.

Solution 3 - Vim

No not directly. What you can do though is quickly exit insert mode for a single normal mode operation with Ctrl-O and then paste from there which will end by putting you back in insert mode.

Key Combo: Ctrl-O p

EDIT: Interesting. It does appear that there is a way as several other people have listed.

Solution 4 - Vim

While in insert mode, you can use Ctrl-R {register}, where register can be:

  • + for the clipboard,
  • * for the X clipboard (last selected text in X),
  • " for the unnamed register (last delete or yank in Vim),
  • or a number of others (see :h registers).

Ctrl-R {register} inserts the text as if it were typed.

Ctrl-R Ctrl-O {register} inserts the text with the original indentation.

Ctrl-R Ctrl-P {register} inserts the text and auto-indents it.

Ctrl-O can be used to run any normal mode command before returning to insert mode, so
Ctrl-O "+p can also be used, for example.

For more information, view the documentation with :h i_ctrl-r

Solution 5 - Vim

You can use this to paste from clipboard with Ctrlv:

set pastetoggle=<F10>
inoremap <C-v> <F10><C-r>+<F10>

And this for yanking visual selection into clipboard with Ctrlc:

vnoremap <C-c> "+y

If you also want to use clipboard by default for classic vim yanking/pasting (y/p) in normal mode, here is a config option that does it:

set clipboard=unnamedplus

With this configs you can e.g. yank first in normal mode and then paste with Ctrlv in insert mode. Also, you can paste text from different vim instances and different applications.

Another option is:

set clipboard=unnamed

Then you will be able to just select something by mouse dragging in your X environment and paste it into vim afterwards. But (for some reason) you won't be able to yank something (y) in Vim and shiftinsert it somewhere else afterwards, which is probably quite limiting.

Vim docs about this: http://vim.wikia.com/wiki/Accessing_the_system_clipboard

For pasting from custom registers you can follow the other answers :). This answer is mainly about integrating Vim with your system clipboard.


Note that for set clipboard=unnamedplus and set clipboard=unnamed to work, you need to use gvim or vimx (vim-X11): Those are compiled with +xterm_clipboard. You can optionally put this into your .bashrc to alias vim with vimx:

if [ -e /usr/bin/vimx ]; then
    alias vim='/usr/bin/vimx'; # vim with +xterm_clipboard
fi

You can find out whether or not your vim has the +xterm_clipboard in the information provided by vim --version.

Solution 6 - Vim

If you set Vim to use the system clipboard (:set clipboard=unnamed), then any text you copy in Vim can be pasted using Shift + Insert. Shift + Insert is simply an OS-wide paste key-combination (Ctrl + Insert is the corresponding 'copy').

Solution 7 - Vim

You can also use the mouse middle button to paste in insert mode (Linux only).

Solution 8 - Vim

You can enter -- INSERT (past) -- mode via:

  • Keyboard combo: y p

or

  • :set paste and entering insert mode (:set nopaste to disable)

once in -- INSERT (past) -- mode simply use your systems paste function (e.g. CtrlShiftv on Linux, Cmdv on Mac OS).

This strategy is very usefully when using vim over ssh.

Solution 9 - Vim

Yes. In Windows Ctrl+V and in Linux pressing both mouse buttons nearly simultaneously.

In Windows I think this line in my _vimrc probably does it:

source $VIMRUNTIME/mswin.vim

In Linux I don't remember how I did it. It looks like I probably deleted some line from the default .vimrc file.

Solution 10 - Vim

Paste in Insert Mode

A custom map seems appropriate in this case. This is what I use to paste yanked items in insert mode:

inoremap <Leader>p <ESC>pa

My Leader key here is \; this means hitting \p in insert mode would paste the previously yanked items/lines.

Solution 11 - Vim

Just add map:

" ~/.vimrc
inoremap <c-p> <c-r>*

restart vim and when press Crtl+p in insert mode, copied text will be pasted

Solution 12 - Vim

Add this to vimrc or init file:

imap <silent> PP <ESC>pa

..to paste in insert mode with "PP" and stay in insert mode..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmir RachumView Question on Stackoverflow
Solution 1 - VimdavidView Answer on Stackoverflow
Solution 2 - VimJames SnyderView Answer on Stackoverflow
Solution 3 - VimJaredParView Answer on Stackoverflow
Solution 4 - VimZazView Answer on Stackoverflow
Solution 5 - VimclimeView Answer on Stackoverflow
Solution 6 - VimWillView Answer on Stackoverflow
Solution 7 - VimlcltjView Answer on Stackoverflow
Solution 8 - Vimrudolph9View Answer on Stackoverflow
Solution 9 - VimWindows programmerView Answer on Stackoverflow
Solution 10 - VimSheharyarView Answer on Stackoverflow
Solution 11 - VimitsnikolayView Answer on Stackoverflow
Solution 12 - VimarilebedeyView Answer on Stackoverflow