Highlight yanked region

1 Point

Cem Aksoylar Cem Aksoylar

5 years ago

Briefly highlight the part of the buffer that was yanked. Does not work for blockwise yanks, e.g. yanking a region selected with visual block mode :h v_CTRL-V. Also may flash the screen briefly due to redraw!, without which highlighting may not always appear.

function! s:hl_yank() abort
    let [sl, el, sc, ec] = [line("'["), line("']"), col("'["), col("']")]
    if sl == el
        let pos_list = [[sl, sc, ec - sc + 1]]
    else
        let pos_list = [[sl, sc, col([sl, "$"]) - sc]] + range(sl + 1, el - 1) + [[el, 1, ec]]
    endif

    for chunk in range(0, len(pos_list), 8)
        call matchaddpos('IncSearch', pos_list[chunk:chunk + 7])
    endfor
    redraw!
    call timer_start(500, {t_id -> clearmatches()})
endfunction

augroup HlYank
    autocmd!
    autocmd TextYankPost * if v:event.operator ==# 'y' | call s:hl_yank() | endif
augroup END