1

I am wondering if there is a way for Vim to alarm me when I am pasting a large amount of texts (using clipboard) into it.

For example, when I am pasting more than 10000 letters, Vim can alarm me that I am pasting too much and confirm if I would like to proceed.

Is this possible?

Thank you in advance!

6

Try this:

nno <silent> p @=<sid>CustomPaste(v:true)<cr>
nno <silent> P @=<sid>CustomPaste(v:false)<cr>

fu s:CustomPaste(below) abort
    let register_size = strchars(getreg(v:register), v:true)
    if register_size > 10000
        let question = 'You are about to paste a long register.  Do you want to proceed? y/n'
        call popup_dialog(question, {
            \ 'filter': 'popup_filter_yesno',
            \ 'callback': function('s:ConfirmLongPaste', [a:below]),
            \ })
        return ''
    endif
    return a:below ? 'p' : 'P'
endfu

fu s:ConfirmLongPaste(below, id, result) abort
    if a:result == 1
        call feedkeys(a:below ? 'p' : 'P', 'nxt')
    endif
endfu

Or this:

vim9

nno <expr> p <sid>CustomPaste()
nno <expr> P <sid>CustomPaste(v:false)

def CustomPaste(below = true): string
    var register_size = getreg(v:register)->strchars(true)
    if register_size > 10'000
        var question = 'You are about to paste a long register.  Do you want to proceed? y/n'
        popup_dialog(question, {
            filter: 'popup_filter_yesno',
            callback: function(ConfirmLongPaste, [below]),
            })
        return "\<ignore>"
    endif
    return below ? 'p' : 'P'
enddef

def ConfirmLongPaste(below: bool, _: any, result: number)
    if result == 1
        feedkeys(below ? 'p' : 'P', 'nxt')
    endif
enddef

For more info, see:


You need at least 8.1.1548 for the popup functions to be available.

For older versions, try :h confirm() instead:

nno <silent> p @=<sid>CustomPaste(1)<cr>
nno <silent> P @=<sid>CustomPaste(0)<cr>

fu s:CustomPaste(below) abort
    let register_size = strchars(getreg(v:register))
    if register_size > 10000
        let question = 'You are about to paste a long register.  Do you want to proceed? y/n  '
        if confirm(question, "&Yes\n&No\n&Cancel") == 1
            call feedkeys(a:below ? 'p' : 'P', 'nxt')
        endif
        return ''
    endif
    return a:below ? 'p' : 'P'
endfu
  • Thanks for the answer, this unfortunately did not work form me, I am using Vim 7.4, could this be the reason? – zyy Jan 6 at 20:01
  • Try the code at the end. I checked that it works on 7.4.001. – user938271 Jan 6 at 20:23
  • Wow, great answer! – Christian Brabandt Jan 7 at 9:54
  • @user938271 Sorry, I was not clear, I mean copying from clipboard using command + V, but thank you so much! – zyy Jan 8 at 0:06

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.