57

Someone showed me how to do this before but I can't figure out what it was now.

I know about :set paste but this is not the problem.

5
  • Maybe the problem is that Vim keeps trying to redraw the screen while you paste. Maybe there's a way to temporarily disable updating of the screen. Commented Aug 15, 2013 at 18:44
  • 2
    @RoryO'Kane maybe set lazyredraw?
    – FDinoff
    Commented Aug 15, 2013 at 18:52
  • @FDinoff lazyredraw affects macros and commands, not typed text. Commented Aug 15, 2013 at 18:56
  • 1) how huge the text is? 2) did you paste to a buffer with some syntax highlighting? e.g. paste a big trunk of html codes to your current HTML buffer...
    – Kent
    Commented Aug 16, 2013 at 8:30
  • 3
    I'm on a mac and used pbpaste > file.txt. I know this isn't vim specific, but was super fast
    – jOshT
    Commented Jan 4, 2018 at 21:56

5 Answers 5

107

Use "*p or "*P to paste from the system clipboard instantly.

Vim must be compiled with +clipboard for this to work.

See :help clipboard for more information.

7
  • 9
    If the '* register doesn’t work, you can also try "+. Paste from it with "+p. Commented Aug 15, 2013 at 18:57
  • 4
    how I am gonna compile vim with +clipboard ?
    – HMagdy
    Commented Jul 29, 2017 at 13:31
  • 3
    On Ubuntu 18.04, sudo apt install vim-gtk installs Vim with clipboard support! (found here) Commented Jul 22, 2018 at 5:10
  • What is the difference between "+ and "* ?
    – minseong
    Commented Dec 8, 2019 at 6:00
  • 2
    @minseong It seems "* holds the "mouse selection" clipboard (what you paste with middle click on Linux), while "+ holds the main clipboard (what you paste with Ctrl+C).
    – Jellby
    Commented Aug 11, 2022 at 9:27
24

This is a buffer flush-to-disk problem. Vim tries to keep your work safe and doesn't assume you can type several thousand characters per second. Read :help swap-file for some details on the buffering. The solution to your problem is this:

Turn off vim's swapfile either with:

vim -n <your file>

or from within vim before the paste:

:set noswapfile

See :help swapfile for more details.

Another option is to simply turn off the syncing to disk of the swap file with :set swapsync= but this option takes more keystrokes to undo and I'm lazy. :)

Turning off swap is not safe for normal operations! Immediately after the paste, either use :set swapfile or :set swapsync=fsync to revert back to normal behavior (though technically, normal behavior might have been sync and not fsync, check with :set swapsync? beforehand if you want to go this route).

1
  • 2
    I have set noswapfile in my vimrc but still pasting is slow, do I need to set something else too? Commented Jan 8, 2021 at 11:32
11

In my experience :set syntax=off helps before pasting, see https://askubuntu.com/a/223061/240577 I've seen 'slow' behavior/high cpu usage with vim/gvim/mvim on osx, ubuntu, redhat and windows when pasting large blocks of content and syntax highlighting is on.

3

I use :read !xclip -o in my vim document after I positioned my cursor to paste my copied text.

Could also be:

:read !xsel -b # X11
:read !pbpaste # On macos
:r !@xclip -o # :r as :read alias
0

For some filetypes :set syntax=off makes no difference because the slowdown is caused by foldexpr. Setting an empty foldexpr makes pasting fast again.

Doing it manually is stupid (and slow!). Luckily vim supports bracketed paste in many terminals and enables paste automatically, so it's possible to hook into that and temporarily disable foldexpr:

function! s:paste_toggled(new, old) abort
    if a:new && !a:old
        let b:saved_foldexpr = &foldexpr
        let &l:foldexpr = ''
    elseif !a:new && a:old && exists('b:saved_foldexpr')
        let &l:foldexpr = b:saved_foldexpr
        unlet b:saved_foldexpr
    endif
endfunc

augroup FastPaste
    autocmd OptionSet paste call s:paste_toggled(v:option_new, v:option_old)
augroup END

(source: https://github.com/liskin/dotfiles/blob/69ab04e64e001da90232eaf2c5856337d58c785f/.vim/plugin/fastpaste.vim)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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