Skip to content
Theme:

Highlight yanked text in Neovim

Before Neovim, I used to use Helix, which follows the same editing model as Kakoune. Having experience with both, I must admit that I prefer Helix’s selection → action editing model. It is a lot more precise and less error-prone to see the selection before taking action upon it. Although this workflow is possible in Vim, it can be inefficient.

Opinions and code editor disputes aside, there is a little trick in Neovim that enables nice feedback after yanking a portion of text. Look at the example and a little Lua recipe.

A post yank highlight feedback emitted after yanking a word, line and a code block.
vim.api.nvim_create_autocmd("TextYankPost", {
    callback = function()
        vim.highlight.on_yank()
    end,
})

The highlighting group styling, timeout, and a few other things are configurable. It’s a little thing, but I found it superbly useful.


While writing this post and browsing Neovim’s events reference, I came across the UserGettingBored event. It doesn’t do anything, but it is cool to see a sense of humour spread throughout the docs 🤣

Comments

  • k
    kaomoji

    "This is such a simple but super useful tip! I totally get where you're coming from—coming from Helix, having that immediate visual feedback when you yank text is something I definitely miss in standard Neovim setups. Adding that little snippet of Lua to get the highlight.on_yank feedback is such a smart way to make the editing experience feel much more polished. Thanks for sharing, definitely adding this to my config!

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!

Leave a comment

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!