Abort a vim paste

Sometimes I paste a lot of data into vim (>5000 rows) and forget to enable paste mode. In that case, vim is busy for several minutes, trying to correctly indent all the text (which gets REALLY bad performancewise with some kind of data, indenting it by some 10'000 chars).

Is there a way to abort the pasting process? ESC, CTRL-C and CTRL-D don't work.


Solution 1:

If you are using the GUI version, you really should paste from either the clipboard register ("*) or the X11 selection ("+), as already suggested by echristopherson. So instead of middle-clicking or whatever, you navigate where you want to paste and then type "*P, including the initial quote. When pasting via the p or P commands, vim knows that you are pasting, hence the 'paste option is unnecessary—no matter from which register you are pasting.

If you are pasting from the terminal, vim doesn't really have a say in the matter. The terminal emulator will dump whatever you have instructed it and in most cases vim cannot differentiate whether the characters that keep coming in are you typing or you pasting. Hence you need to set 'paste to tell vim that it shouldn't do indenting and stuff.

As a consequence, if you are pasting from the terminal, all the characters that are to be pasted are queued in one go and are essentially already sent to vim, so there is really nothing that you can do to interrupt it. From the point of view of the terminal emulator, CTRL+c is but an ordinary character, therefore it gets queued behind all the other stuff that is waiting to be pasted. When vim finally gets to see the CTRL+c, it is too late, as there is nothing to be aborted anymore.

( Update: Depending on the terminal emulator, CTRL+c and similar signals may be sent in a prioritised way. Specifically, I've noticed this in Putty, which delivers the break signal instantly and indeed allows me to abort misplaced lengthy paste operations. I don't really know how this works, though. )


Long story short: If at all possible, use "*P or "+P to paste (which even works in some terminal emulators if I recall correctly). This is the "correct" way to paste; everything else is basically a workaround gives you those kinds of headaches.