Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pasting from the clipboard and automatically toggling ":set paste"

Tags:

vim

paste

When I paste things from the clipboard, they're normally (always) multilined, and in those cases (and those cases only), I'd like :set paste to be triggered, since otherwise the tabbing will increase with each line (you've all seen it!).

Though the problem with :set paste is that it doesn't behave well with set smartindent, causing the cursor to jump to the beginning of a new line instead of at the correct indent. So I'd like to enable it for this instance only.

I'm using Mac, sshing to a Debian machine with Vim, and thus pasting in Insert mode using cmd + v.

like image 276
Jonatan Littke Avatar asked Mar 31 '10 18:03

Jonatan Littke


1 Answers

I don't use a mac, but I believe I have the prefix right here: <D-v> should mean cmd-v. For insert mode:

:imap <D-v> ^O:set paste<Enter>^R+^O:set nopaste<Enter>

or really, just do this:

:imap <D-V> ^O"+p

The ^O and ^R are literal control-O and control-R, which you can type with ^V^O (control-v control-o) and ^V^R (control-v control-r). Control-O in insert mode allows you to execute one command then return to insert mode; here you can use it to put from the clipboard register.

This worked for me when I tested them mapped to a different key, so you should be all set.

There's no need to map anything when not in insert mode; you can just use "+p.

like image 196
Cascabel Avatar answered Nov 07 '22 03:11

Cascabel