Home

Vim. Copying and pasting

Buffers, registers

Pasting

  • Insertion mode
    • crtl+r ": from normal register
  • Normal mode
    • p: from normal register

Pasting from a register.

  • Insertion mode
    • ctrl+r letter_of_your_register
  • Normal mode
    • " letter_of_your_register p

Copying

In normal mode:

  • using mouse
  • select with v then y
  • copy word yw, y2w

Copying to a register: select your text with v and then "qy with q being an example of a register letter

Pasting

Start thinking in registers. Copying in pasting in vim is more complex than ctrl-c ctrl-v, be it for better or worse.

The most important things to keep in mind:

  • y: yank (yw - yank word)
  • p: paste
  • ctrl+"
  • * register: all vim deletes, cuts and yanks go into this register
  • + register: all OS copies go into this register
  • 0 register: only yanks will go into this register. Have you ever been frustrated with yanking something, then going to another line, deleting the unwanted word and then pasting with p the deleted word instead of the yanked? so next time you delete something with dw, use ctrl+" 0 to paste what you yanked and not what you deleted

https://stackoverflow.com/questions/2514445/turning-off-auto-indent-when-pasting-text-into-vim
To avoid messed up indentation after pasting use:

:set paste

Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) --.

And turn it off after:

:set nopaste

However, I always found that cumbersome. That's why I map < F3 > such that it can switch between paste and nopaste modes while editing the text! I add this to .vimrc

set pastetoggle=<F3>