[vim/vim] HighlightedYank feature (Issue #14848)

53 views
Skip to first unread message

giri

unread,
May 25, 2024, 9:19:13 AMMay 25
to vim/vim, Subscribed

Now that (getregionpos())[https://github.com/vim/vim/pulls?q=is%3Apr+getregionpos] has been merged, it is quite straightforward to have (this feature)[https://github.com/machakann/vim-highlightedyank]. The plugin temporarily highlights the yanked region to make it apparent. I use this feature, and it may be of value to others. It is also natively included in Neovim.

If enough people think this is worthwhile I can submit a PR. Maybe this can be a plugin, or an example added to the help file next to getregionpos().

def HighlightedYank(hlgroup: string = 'IncSearch', duration: number = 300)
    if v:event.operator ==? 'y'
        var [beg, end] = [getpos("'["), getpos("']")]
        var type = v:event.regtype[0] ?? 'v'
        var pos = getregionpos(beg, end, {type: type})
        var end_offset = (type == 'V' || v:event.inclusive) ? 1 : 0
        var m = matchaddpos(hlgroup, pos->mapnew((_, v) => {
            var col_beg = v[0][2] + v[0][3]
            var col_end = v[1][2] + v[1][3] + end_offset
            return [v[0][1], col_beg, col_end - col_beg]
        }))
        timer_start(duration, (_) => m->matchdelete())
    endif
enddef
autocmd TextYankPost * HighlightedYank()


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848@github.com>

bfrg

unread,
May 25, 2024, 11:41:58 AMMay 25
to vim/vim, Subscribed

You need to pass the window ID of the current window (the currently focused window when timer_start() is called) to matchdelete() since the user might have changed the focus to another window when matchdelete() is invoked. Otherwise matchdelete() will try to delete the highlighting in the wrong window.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2131312136@github.com>

bfrg

unread,
May 25, 2024, 11:45:16 AMMay 25
to vim/vim, Subscribed

And I don't quite understand how getregionpos() makes this simpler. It worked perfectly fine with line() and col() using the '[ and '] marks.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2131312839@github.com>

Christian Brabandt

unread,
May 25, 2024, 2:15:46 PMMay 25
to vim/vim, Subscribed

Thanks. Can some people try this out? I'd like to know how well this works. Perhaps we can add it to the help of vim9 script. Not sure it makes sense to include this as a plugin however.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2131382341@github.com>

zeertzjq

unread,
May 25, 2024, 4:32:57 PMMay 25
to vim/vim, Subscribed

type can contain the width of a blockwise selection now, so the [0] in regtype[0] can be removed. Specifying a width for a blockwise selection also seems the only way to indicate one that stops at end of line ($).


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2131430491@github.com>

zeertzjq

unread,
May 25, 2024, 9:31:02 PMMay 25
to vim/vim, Subscribed

Also the value of end_offset seems wrong. The column values of end positions returned by getregionpos() is always inclusive, so end_offset should always be 1.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2131896803@github.com>

giri

unread,
May 26, 2024, 2:56:45 AMMay 26
to vim/vim, Subscribed

type can now contain the width of a blockwise selection, so the [0] in regtype[0] can be removed. Specifying a width for a blockwise selection also seems the only way to indicate one that stops at end of line ($) when a line in the middle is longer than the lines at the start and the end.

If I remove [0], I get E475: Invalid value for argument type: ^V3 (3 is just width) when I do visual region select and yank (<c-v> to select region and y). This is on the main branch, coming from getregionpos().


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2132093860@github.com>

zeertzjq

unread,
May 26, 2024, 3:00:22 AMMay 26
to vim/vim, Subscribed

Do you Vim version contain patch 9.1.0443?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2132095681@github.com>

giri

unread,
May 26, 2024, 3:02:52 AMMay 26
to vim/vim, Subscribed

Also the value of end_offset seems wrong. The column values of end positions returned by getregionpos() is always inclusive, so end_offset should always be 1.

Word movement w does not work with your suggestion. It includes 1 extra character to the right. You can try yw and verify.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2132097295@github.com>

zeertzjq

unread,
May 26, 2024, 3:05:54 AMMay 26
to vim/vim, Subscribed

Oops, sorry, I confused two different flags.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2132098430@github.com>

giri

unread,
May 26, 2024, 3:11:17 AMMay 26
to vim/vim, Subscribed

Does your Vim version contain patch 9.1.0443?

I made a mistake when I did 'make install'. It works as your patch intended. Removed [0].


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2132100556@github.com>

lkintact

unread,
May 29, 2024, 6:54:56 AMMay 29
to vim/vim, Subscribed

The line
autocmd TextYankPost * HighlightedYank()
should perhaps be replaced with
autocmd TextYankPost * call HighlightedYank().

Otherwise after sourcing the script and yanking Vim produces an error:

Error detected while processing TextYankPost Autocommands for "*":
E492: Not an editor command: HighlightedYank()

Vim 9.1.443.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2137125933@github.com>

bfrg

unread,
May 29, 2024, 2:02:24 PMMay 29
to vim/vim, Subscribed

The line
autocmd TextYankPost * HighlightedYank()
should perhaps be replaced with
autocmd TextYankPost * call HighlightedYank().

The first one is the vim9script version where call can be omitted. You need to add vim9script at the top of the file.

The autocmd should be put inside an augroup:

vim9script

augroup highlightedyank
    autocmd!
    autocmd TextYankPost * HighlightedYank()
augroup END

Or alternatively using the newer autocmd_add() function:

vim9script

autocmd_add([{
    group: 'highlightedyank',
    event: 'TextYankPost',
    pattern: '*',
    cmd: 'HighlightedYank()',
    replace: true
}])


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2137976904@github.com>

lkintact

unread,
May 30, 2024, 2:58:44 AMMay 30
to vim/vim, Subscribed

@bfrg: I see, thank you!


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2138812937@github.com>

Nick Jensen

unread,
May 30, 2024, 6:29:23 AMMay 30
to vim/vim, Subscribed

Thanks. Can some people try this out? I'd like to know how well this works.

I've been using this and it works great.

Perhaps we can add it to the help of vim9 script. Not sure it makes sense to include this as a plugin however.

I started using machakann/vim-highlightedyank a few months ago, thinking I'd try it for fun, and have really come to rely on it. It's a simple thing, but the quick visual guide to confirm that the yank you intended was successful is extremely useful. More importantly, I really notice if the yank doesn't highlight (because I fat-fingered the motion, for example), and that saves me time mucking around pasting the wrong thing.

So in my opinion a simple script like this would be a good addition as a built-in plugin. It adds nice usability value with a small amount of code, similar to the comment plugin that was added recently.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/14848/2139252880@github.com>

Christian Brabandt

unread,
May 31, 2024, 9:12:19 AMMay 31
to vim/vim, Subscribed

Closed #14848 as completed via bad9577.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/14848/issue_event/13000445330@github.com>

Reply all
Reply to author
Forward
0 new messages