vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

hlyank.vim (2008B)


      1 vim9script
      2 
      3 # Highlight Yank plugin
      4 # Last Change: 2026 May 11
      5 
      6 def HighlightedYank()
      7 
      8   var hlgroup = get(g:, "hlyank_hlgroup", "IncSearch")
      9   var duration = min([get(g:, "hlyank_duration", 300), 3000])
     10   var in_visual = get(g:, "hlyank_invisual", true)
     11 
     12   if v:event.operator ==? 'y'
     13     if !in_visual && visualmode() != null_string
     14       visualmode(1)
     15       return
     16     endif
     17     # if clipboard has autoselect (default on linux) exiting from Visual with
     18     # ESC generates bogus event and this highlights previous yank
     19     if &clipboard =~ 'autoselect' && v:event.regname == "*" && v:event.visual
     20       return
     21     endif
     22     var [beg, end] = [getpos("'["), getpos("']")]
     23     var type = v:event.regtype ?? 'v'
     24     var pos = getregionpos(beg, end, {type: type, exclusive: false})
     25     var m = matchaddpos(hlgroup, pos->mapnew((_, v) => {
     26       var col_beg = v[0][2] + v[0][3]
     27       var col_end = v[1][2] + v[1][3] + 1
     28       return [v[0][1], col_beg, col_end - col_beg]
     29     }))
     30     var winid = win_getid()
     31     timer_start(duration, (_) => {
     32       if winbufnr(winid) != -1
     33         m->matchdelete(winid)
     34       endif
     35     })
     36   endif
     37 enddef
     38 
     39 export def HighlightedPut()
     40     if !get(g:, "hlput_enable", false)
     41       return
     42     endif
     43 
     44     var hlgroup = get(g:, "hlput_hlgroup", "IncSearch")
     45     var duration = min([get(g:, "hlput_duration", 300), 3000])
     46 
     47     var [beg, end] = [getpos("'["), getpos("']")]
     48     var type = v:event.regtype ?? 'v'
     49     var pos = getregionpos(beg, end, {type: type, exclusive: false})
     50 
     51     var m = matchaddpos(hlgroup, pos->mapnew((_, v) => {
     52         var col_beg = v[0][2] + v[0][3]
     53         var col_end = v[1][2] + v[1][3] + 1
     54         return [v[0][1], col_beg, col_end - col_beg]
     55     }))
     56     var winid = win_getid()
     57     timer_start(duration, (_) => {
     58         if winbufnr(winid) != -1
     59             m->matchdelete(winid)
     60         endif
     61     })
     62 enddef
     63 
     64 augroup hlyank
     65   autocmd!
     66   autocmd TextYankPost * HighlightedYank()
     67   autocmd TextPutPost * HighlightedPut()
     68 augroup END
     69 # vim:sts=2:sw=2:et: