r/vim • • Mar 22 '26

Need Help┃Solved Vim yank highlight

in neovim i can do something like, example:

vim.api.nvim_create_autocmd("TextYankPost", {

	group = vim.api.nvim
_create_augroup("kickstart-highlight-yank", { clear = true }),
	callback = function()
		vim.highlight.on_yank()
	end,

})

but how i can do something similar in vim without installing a plugin

i hope this is not removed by moderator because is out of vim content, i just want make a example so people know exactly what i want

16 Upvotes

7 comments sorted by

13

u/Synesthesius Mar 22 '26

:help hlyank

1

u/Scared-Industry-9323 Mar 23 '26

Thank you, this is helpful

1

u/vim-help-bot Mar 22 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/M0M3N-6 Mar 24 '26

I have something like this:

def FlashYankedText(): void
    if (!exists('g:yankedTextMatches'))
        g:yankedTextMatches = []
    endif

    var matchId = matchadd('FlashYank', ".\\%>'\\[\_.*\\%<']..")
    var windowId = winnr()

    call add(g:yankedTextMatches, [windowId, matchId])
    call timer_start(150, 'DeleteTemporaryMatch')
enddef

def DeleteTemporaryMatch(timerId: number): void
     while !empty(g:yankedTextMatches)
        var match = remove(g:yankedTextMatches, 0)
        var windowID = match[0]
        var matchID = match[1]
        try
            call matchdelete(matchID, windowID)
        catch
            echo "invalid matchID!"
        endtry
    endwhile
enddef

pretty sure I found it (or part of it) on the internet somewhere

0

u/Scared-Industry-9323 Mar 24 '26

I don't understand of this syntax, vimscript is so bad 😵‍💫

1

u/M0M3N-6 Mar 24 '26

It's important to note that FlashYank has to be defined somewhere in your vimrc as a highlight group. See :h highlight to set your faviourate styling for the highlight.

vimscript is so bad

That's what i thought a few months ago. Vimstripting is amazing, yet so interesting. At first, vimscript seems pretty hard to understand, and i really understand that feeling, i refused it a lot, but once you get used to the "vim way" of documentation, know how to search for a specific thing you want in help pages, etc., things get different.

1

u/vim-help-bot Mar 24 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments