I have been using the paste mappings from vim-unimpaired by Tim Pope, which will drop you on insert mode under set paste
and restore nopaste
as soon as you leave insert mode.
The mappings are:
[op
: Paste above the current line (similar to O
but with set paste
)
]op
: Paste below the current line (similar to o
but with set paste
)
yop
: Paste replacing the current line (similar to S
but with set paste
)
So one could use the sequence:
Since leaving Insert mode with "Esc" is enough to restore nopaste
mode.
But I also like to paste from when I'm already in Insert mode, for example if I want to write something above the text I'm about to paste.
For that, I use the following mapping:
inoremap <C-e> <C-g>u<C-o>:normal <Plug>unimpairedPaste<cr>
This will use the Ctrl+E key in Insert mode to switch to set paste
mode, while setting up the restoring of nopaste
after leaving Insert mode. It reuses the internal mapping from vim-unimpaired. Before setting up for pasting, it issues a <C-g>u
sequence, to break the "undo" tree, meaning you can undo the paste operation on its own, without undoing the additional text you wrote before your paste.
So then the sequence is:
i
(or similar), to enter Insert mode
- enter any text that should precede the paste
- Ctrl+E, to
set paste
- Cmd+P, to actually paste the contents of the clipboard
- Esc, to leave Insert mode
:set paste
before Cmd+P and:set nopaste
after it?paste!
but yes I mixed them up (fixed now)