Reputation: 2123
I want to know if it's possible to trigger a method whenever text is copied in vim. The method should be triggered on all the yank event, so overwriting keymap of "yy" and "yiw" isn't the answer for me (because you know, we cant write infinity number of keymapping like "yfx", "y3l" etc)
And to any of you who has the answer, i also want to ask another question. In the call back method of yank event, can I know which text is yanked? (e.g. 10th line , from 3rd character to 8th character)
Upvotes: 2
Views: 307
Reputation: 8905
I think you will need to take an alternate approach for your plugin.
The last yank is stored in the @0
register, so if you monitor that register for changes, you can detect when something new got yanked.
Use a CursorHold,CursorHoldI and/or CursorMoved autocmd to compare the current value of @0
to a stored value, and to update the stored value and do the highlight when a change occurs.
Note, this won't work if the user specifies a specific named register for their yank. You could monitor all of those, too; or map y to capture the register used from v:register
and monitor that; however these registers are often used for macros, deletes, etc. as well as yanks.
Upvotes: 2