Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

As far as I know, there is no autocmd event for copying and pasting. So I was curious if it is possible to write a script that toggles paste mode when the clipboard is used for paste. If it can handle Shift+Insert, Ctrl+Shift+v and middle-mouse button, or even internal script, that would be awesome, but I am primarily seeking to solve the case with keyboard shortcuts. My vim has -clipboard attribute.

share|improve this question
    
Is there a valid reason why you don't use "+y? –  romainl Mar 11 '13 at 9:07
    
I combined the X11 'primary' clipboard and 'clipboard' clipboard on my OS and also my vim is configured to copy everything to special registers automatically. It's actually very convenient this way. There is a bug that needs to be worked around but I recommend you to give clipIt (and fakeclip if you lack +clipboard attr in your vim) a try! –  Forethinker Mar 11 '13 at 16:56
    
(hmm, why did I say y instead of p?) Nevermind, my point is that, if you use p (either with "+/"* or without a register if you have set clipboard^=unnamedplus), the pasted text is always how you expect it to be. <kbd>Shift+Insert</kbd>, <kbd>Ctrl+Shift+v</kbd> and middle-mouse button are the wrong way to paste text in Vim. p is the right way. Unless you work remotely. –  romainl Mar 11 '13 at 19:53
    
clipboard variable only exists for vim with +clipboard attribute. –  Forethinker Mar 11 '13 at 19:56
    
It's both easier and faster to install a proper Vim with clipboard support than messing around with external tools and custom mappings. Nothing beats p (and P). –  romainl Mar 11 '13 at 20:16

3 Answers 3

up vote 2 down vote accepted

When you use GVIM or paste using the p / i_CTRL-R commands together with a register "* or "+, Vim recognizes the paste, and everything should work just fine. What you're referring to is presumably the 'pastetoggle' setting, which is used to toggle "paste" mode when pasting through the terminal (e.g. with middle mouse button or a terminal shortcut). The reason that mode and toggle mapping exist is exactly that Vim is not able to detect whether the text comes from the user typing or a paste.

You could only somehow teach your terminal to send Vim's 'pastetoggle' key before and after pasting (when Vim is currently running); this cannot be solved in Vim alone.

share|improve this answer
    
I have to disagree with the statement "Vim is not able to detect..." When you paste text it is received at a characters per second rate far exceeding what a human could ever type. Instead of setting a pastetoggle key, it would be far more useful to set a pastedetectthreshold. But that feature doesn't seem to exist. –  Bruno Bronosky Aug 5 at 15:27

My question is actually flawed, as it does not reflect exactly what I wanted, but now I have what I want. At least in my setup (set nopaste, set autoindent, copyindent, smarttab are set), I get paste style paste with p or P and nopaste style paste with Ctrl-Shift-v in insert mode. If I paste with Ctrl-Shift-v in normal mode, I get a weird behavior.
My settings may not mimic pastetoggle settings identically and it does not answer the question in the title, but it solves the problem I had. I've tested in both in mintty and terminator.

share|improve this answer

I have below map set in vim:

set mouse=inv
noremap <MiddleMouse> :set paste<CR>"*p:set nopaste<CR>

You can add this line in your ~/.vimrc or ~/.exrc.

I think, this is pretty straightforward & would not require much explanation.
(Re)map middlemouse to set paste mode, then paste contents of * register & then unset paste mode.
You need mouse support for this.

Note: As already suggested in comments on the question, is not the correct way to paste text in vim. But we have now got a habit of pasting with on linux. So, this mapping comes handy.

Tested on vim 7.4 on gnome-terminal via putty+xming.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.