When I am inserting a big block of code or text, often I will want to copy and paste text from elsewhere into that block.
Is the only way to do this to leave insert mode, go get the text and paste, then go back to insert mode?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityYou don't have to leave insert mode if you don't want to. While in insert mode, paste text from a register by pressing <C-r>registername
, e.g. <C-r>+
to paste from system clipboard or <C-r>"
to paste lines yanked inside vim (from the unnamed register).
Edit:
After the clarification in your comment, I would recommend that you adapt a different workflow in vim:
While being in insert mode, just type the beginning of your long constant or variable name, e.g. the first 3 characters followed by <C-p>
. Vim will use its built-in autocompletion to complete the word. If more than one word starts with the same sequence, press <C-p>
repeatedly (or use <C-n>
to select previous item).
I'm not exactly sure what you want to do here, but I think you're looking for :h i_ctrl-r
.
If you're in insert mode, and need to copy text from a webbrowser or other application (or window). Do the copy, return to vim, and type <C-R>*
or <C-R>+
.
If you need to insert something yanked earlier in vim while staying in insert mode, you might be looking for <C-R>"
instead
If you want to use vim like a traditional, notepad-like editor, you can use select mode. This is like visual mode, but with your mouse, and it behaves more like a traditional editor does. Add these to your .vimrc
set selectmode+=mouse
snoremap <C-v> "+y
snoremap <C-x> "+d
Then for paste, do this:
inoreamp <C-v> <C-o>"*p
Personally, I recommend against mapping to <C-v>
since this is really useful in insert mode. I would map it to <M-v>
(alt+v) but it's up to you.
:P
In that case, I would just use <C-p>
like cbaumhardt recommended.
May 24, 2016 at 21:39
copy and paste text from elsewhere
Do you mean elsewhere in vim, e.g. in the same file, or elsewhere as in from your browser?