If you use the paste command from your terminal emulator program, Vim will interpret the stream of characters as if they were typed. That will cause all kind of undesirable effects, particularly bad indendation.
To fix that, from command mode:
:set paste
Then move on to insert mode, with i, for example. Notice the mode is now -- INSERT (paste) --
. Now paste with your terminal emulator command, or with the mouse. When finished go to command mode, with Esc and run:
:set nopaste
There is a simpler way, when one wants to paste just once. Put this in your .vimrc (or use the plugin unimpaired.vim):
function! s:setup_paste() abort
set paste
augroup unimpaired_paste
autocmd!
autocmd InsertLeave *
\ set nopaste |
\ autocmd! unimpaired_paste
augroup end
endfunction
nnoremap <silent> yo :call <SID>setup_paste()<CR>o
nnoremap <silent> yO :call <SID>setup_paste()<CR>O
Now one can simply press yo
to paste code under the cursor, and then <Esc>
to go back to normal/nopaste mode.