tags:

views:

189

answers:

3

Hey.

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.

Cheers.

A: 

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.

Jefromi
A: 

You're right in that you should only enable 'paste' when you need it. It does more than just affect indenting. You can read everything that it affects in its documentation. A related option that is very useful to ease the use of 'paste' is 'pastetoggle'.

If you were using X-forwarding and a terminal that can properly communicate mouse actions, you could also take advantage of the 'mouse' option. With :set mouse=a, Vim is made aware of what the mouse is doing and therefore won't perform automatic indentation when it receives a multi-line paste via a middle-button mouse click.

Even without the mouse capability, X-forwarding could help because Vim will do the same thing when manually pasting from the clipboard or selection registers ("+ and "* respectively).

jamessan
Thanks.I did ask for help getting X-forwarding and Mouse and what not to work (http://superuser.com/questions/98350/vim-and-mouse-with-ssh-from-mac-to-linux), but never got it all solved. I'll look into pastetoggle, that should solve it. Vim's documentation (and feature set) is really huge.
Jonatan Littke
A: 

I have the following in my .vimrc:

inoremap <S-Insert> <ESC>:setl paste<CR>gi<C-R>+<ESC>:setl nopaste<CR>gi

gi is to start insert mode in the same position as where Insert mode was stopped last time in the current buffer.

Upd:

Jefromi posted better solution. I have tinkered it a bit

inoremap <S-Insert> <ESC>"+p`]a

It inserts clilpboard text and places cursor right after it.

Maxim Kim
That's nice, but that only works on Shift-Insert, right? And that wouldn't work from my Mac, unless I'm mistaken.
Jonatan Littke
It could be easily mapped to whatever you prefer. <F5> for example.
Maxim Kim
@Jonatan Littke: My answer shows how to map this to command-v, and how to do it more efficiently.
Jefromi
2Jefromi: Looks like your last version is better. thx
Maxim Kim