I usually run vi inside windows on a system like X or the Macintosh. The window systems can copy and paste text between windows. Pasting into a vi window may be tricky if you use vi options like wrapmargin or autoindent-the text you paste can be rearranged or indented in weird ways.
I've fixed that with the keymaps below.
If I'm pasting in text that should be copied exactly with no changes,
I go into text-input mode and type CTRL-x.
That shuts off autoindent (
noai) and the wrapmargin (
When I'm done pasting, I type CTRL-n while I'm still in text-input mode.
A different kind of "pasted" input is with CTRL-r. It starts theutility to reformat and clean up lines while I'm pasting them. To use it, go to text-input mode and type CTRL-r. Then paste the text-fmt will read it but not display it. Press RETURN, then CTRL-d to end the standard input to fmt. The reformatted text will be read into your vi buffer.
" Set 'exact' input mode for pasting exactly what is entered: map! ^X ^[:se noai wm=0^Ma " Set 'normal' input mode with usual autoindent and wrapmargin: map! ^N ^[:se ai wm=8^Ma " Read pasted text, clean up lines with fmt. Type CTRL-d when done: map! ^R ^[:r!fmt^M