Occasionally, I paste large amounts of data into vim, using Ctrl+v in my terminal emulator (in Linux). This takes several seconds before vim is responsive again. I have tried two other methods, which work well: either "+P, or temporarily :set noswapfile before pasting.

I have tested launching vanilla vim, using vim -u NONE, and the problem still persists. As a reference, this 5600-character lorem ipsum takes ~3 seconds to paste with Ctrl+v. It's almost instantaneous with "+P.

I only need the paste alternatives with large data and vim. In all other situations I use Ctrl+v. In vim, this also has the added advantage of working in insert mode. It's only after I paste long data, when I regret my life choices.

Is there a way to automatically apply some workaround, so that is it consistently quick to paste with Ctrl+v?

  • 2
    300000 chars? Personally I’d drop to a shell and do pbpaste >> file or equivalent. You might get away with :read !pbpaste but honestly I expect a performance hit with that much input` – D. Ben Knoble Jan 27 '19 at 6:13
  • 1
    I often paste large logs I may get through a webpage in vim so I can search through them properly (so, definitely more than half a page of text) and pasting is instantaneous (I'm on Linux). Maybe try a fresh vim (in a VM maybe, no plugins or vimrc). If pasting is fast there, then reintroduce your vimrc and plugins bit by bit till you find out which one is at fault. – Shahbaz Jan 27 '19 at 7:02
  • 2
    Have you tried :set swapsync=? That should prevent Vim from waiting until the swapfile is completly written. – Christian Brabandt Jan 28 '19 at 12:55
  • 1
    well, for "+ vim knows that the input is coming from the system clipboard while for your other method of pasting it seems vim doesn't know. So it has to process the input as typed. – Christian Brabandt May 7 '19 at 5:46
  • 1
    @Sparhawk It is not clear to me, that your terminal actually uses xterm-bracketed-paste you haven't said what terminal you used and xterm-bracketed-pasted needs terminal support. So my guess is, it is not being used (or not correctly configured), don't know – Christian Brabandt May 7 '19 at 10:13

You can try setting lazyredraw.

| improve this answer | |
  • Unfortunately this makes no difference. – Sparhawk Jan 28 '19 at 0:47

you could try disabling backup/swap files. if pasting the contents into memory is all you want, disabling disk writes might be faster.

from https://stackoverflow.com/q/743150/11374957

set nobackup       "no backup files
set nowritebackup  "only in case you don't want a backup file while editing
set noswapfile     "no swap files

you could do a comparison with/without those settings and see if it's any faster. another option: do the same test, but turn on a disk I/O monitoring app first and see if that shows any meaningful spike (with backup files enabled) vs. drop (with nobackup, nowritebackup, noswapfile).

| improve this answer | |
  • It's maybe twice as faster with those three settings than without, but still considerably slower than "+p. Also, in the long term, I certainly don't want to disable those settings. Given that "+p is near instantaneous with those settings set, I feel like there is some other issue here. – Sparhawk Apr 18 '19 at 5:29
  • Well, have you tried the suggestion with the swapsync setting as I mentioned before? – Christian Brabandt Apr 18 '19 at 6:04
  • @ChristianBrabandt Oops, sorry, I must have missed that. I'm away from this system for a week or so, but I'll have a look when I'm back. – Sparhawk Apr 18 '19 at 11:50

If you're using something like <Ctrl-V> or <Cmd-v> or equivalent you're doing it wrong. Doing that, the text is pasted into the vim buffer, as if you were typing it. That can have a lot of delay because of several vim options such as autoindent, etc.

Typically you can use either "+p or "+]p (this pastes keeping current indent level in mind) to paste text near instantly as it is.

VIM also has a little known Paste Mode, which essentially is a special mode that disables a bunch of vim settings to allow hassle free pasting without having to worry about vim treating the text in some way, shape of form other than just adding to the buffer. You can have a look at :help 'paste' for more info, there is also a helpful setting pastetoggle to assist with easy enabling / disable of paste mode.

However, in my experience just using the paste operator instead of a native <ctrl-v> version is significantly better.

NOTE: It would be more memory & compute efficient to do this outside of vim with shell redirection.

| improve this answer | |
  • I don't get any autoindent when I paste with ctrl+v, possibly because of xterm-bracketed-paste (but as per the comments to the question, we're not sure). Either way, I don't think it's "as if you were typing it" for me. After :set paste, pasting is still as slow as ever, although now the text is outputted to the screen bit-by-bit as it pastes, instead of all at the end. Using the paste operator is preferred, but as per my question, I often forget! – Sparhawk Jun 12 at 4:17

What about just remap ctrl-v in insert mode to paste from system clipboard?

inoremap <c-v> <esc>"+p

Does this work for you?

| improve this answer | |
  • I'm guessing they'd also need to modify their terminal config to pass the Ctrl-V onto Vim, as in their current setup Vim won't ever see the CTRL-V keypress: it will only receive the text inserted by the terminal. – Rich Jan 13 at 15:39
  • Yes, good idea, but if the terminal never sees ctrl+v as paste, then I could never paste into the CLI, for example. – Sparhawk Jan 13 at 23:20
  • @Sparhawk, which terminal are you using? AFAIK, Linux terminal does not support ctrl-v to paste into the terminal. You need to use Shift+Insert. – jdhao Jan 14 at 2:19
  • Terminator, which does support ctrl+v for paste. shift+insert sends the primary selection instead. – Sparhawk Jan 14 at 2:20
  • Then I think you may use another terminal, for example, kitty or Alacritty. – jdhao Jan 14 at 2:25

I'm going to post and accept this as the best solution proposed, since it was a comment over a year ago that was never converted to an answer.

As per Christian Brabandt's comment, :set swapsync= speeds up the paste by a lot.

Benchmarking with a 100 paragraph lorem ipsum (lorem-ipsum-generator -p 100):

  • "+p is near instantaneous
  • ctrl+v takes ~3.6 s
  • ctrl+v with :set swapsync= takes ~0.3 s

So it's not quite as fast as "+p, but it's pretty good.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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