10

I would like to mimic a nice effect found in the game Vim Adventures: When a yank command is done, I would like the yanked area to be highlighted (let's say in red) for a second to show me that my selection was correct.

For example, yy would highlight the current line in red one second, then I would know what was selected.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
user2854544
  • 851
  • 1
  • 11
  • 23
  • 1
    Why don't you simply select visually before yanking? – romainl Sep 26 '14 at 22:55
  • 2
    Because the command yi( is simple to type than vi(y for example. And any way it's always reasuring to see the copy done visualy – user2854544 Sep 26 '14 at 23:05
  • You need to find a balance between confidence and simplicity/speed. – romainl Sep 26 '14 at 23:17
  • +1 for the idea --- I find it quite interesting. However, deleting the highlight by time might be a bit tricky, because as far as I know, vim is known to lack timer/async features (without certain kinds of plugins/patches). – Yosh Sep 27 '14 at 04:36

4 Answers4

11

For Neovim and Vim

There is a plugin named vim-highlightedyank for this, which works both for Vim and Neovim. Install it and it just works. You can also set the highlight duration by adding the following config:

" set highlight to 1000 ms
let g:highlightedyank_highlight_duration = 1000

Neovim only

If you are using nvim 0.5+, they have made this little feature builtin in this pull request.

No plugin is needed in this case. Just install nvim 0.5+ and add the following config to your init.vim:

augroup highlight_yank
    autocmd!
    au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END

See Neovim's documentation on this feature for more.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
jdhao
  • 24,001
  • 18
  • 134
  • 273
  • for latest neovim version do i need to install the plugin or not? – prateek Mar 14 '21 at 01:01
  • 1
    @prateek If you have the latest version of neovim. You can try the neovim-only method. No need to install vim-highlightedyank. – jdhao Apr 12 '21 at 03:10
9

For Neovim >= 0.7

Neovim includes a function for highlighting a selection on yank, see Neovim Lua documentation.

With Neovim version >= 0.7, you can also Neovim API to define autocommand to use it with TextYankPost event. Add this configuration in your init.lua file :

vim.api.nvim_create_autocmd('TextYankPost', {
  group = vim.api.nvim_create_augroup('highlight_yank'),
  desc = 'Hightlight selection on yank',
  pattern = '*',
  callback = function()
    vim.highlight.on_yank { higroup = 'IncSearch', timeout = 500 }
  end,
})
lcheylus
  • 1,217
  • 8
  • 21
1

One vimmer (not me) saw this question and just wrote a plugin for that. Note that this is not a simple solution and probably not for those who prefer to keep vim minimum, but it works like a charm. Also note that this was not meant to be a serious work, thus you can't necessarily seek for a steady support.


plugins to install

Use your preferred way to install these plugins.

Settings

Let's say you want to use Meta+y to "yank and highlight". Write the following settings in your .vimrc.

noremap <expr> <Plug>(yank-highlight) operator#sequence#map("y", "\<Plug>(operator-highlight)")
nmap <A-y> <Plug>(yank-highlight)
vmap <A-y> <Plug>(yank-highlight)

Change <A-y> to whatever keymap (just y might not work) you'd like to use : <Leader>y can be nice, for example.

The highlight can automatically be cleared. Write the following:

let g:operator#highlight#clear_time=2.0

This will clear that highlight in about 2 secs. The interval also depends on :set updatetime?, which defaults to 4000(ms), so if this doesn't seem to clear the highlighting, try setting updatetime to a smaller value.

Yosh
  • 2,512
  • 3
  • 24
  • 30
  • Wow I look forward to test that! Thank you! – user2854544 Sep 27 '14 at 07:10
  • I get an error *E15: Invalid expression: operator#sequence#map("y", "\(operator-highlight)")* What can I do to get around that ? – user2854544 Sep 28 '14 at 15:55
  • Hmm.... I'm not sure, sorry. I get that kind of error when vim-operator-sequence is not installed, so perhaps you can double-check that if you haven't, for one thing? – Yosh Sep 29 '14 at 05:37
  • Ok the plugin is installed I can see its status Ok. – user2854544 Sep 29 '14 at 05:45
  • In that case, sorry but I can't be of help. I've deleted everything except these three plugins and settings from my `.vimrc` and it's still working. The only suggestions I come up with is (because I know little vim script), try with newer vim if you're using an old one (I tried with 7.4.52 from Ubuntu's repository and had no problems) or that kind of thing. Sorry. If `nnoremap operator#sequence#map("y")` fails (which is unlikely because you see its status being OK), it suggests that the problem is not from vim-operator-highlight.. – Yosh Sep 29 '14 at 06:22
0

I use the plugin by markonm called hlyank.vim.

I opened the plugin and changed the time as it only blinked the color on the screen so changed it from 200 to 800. In the autoload folder, I opened hlyank.vim and edited the following lines:

  call timer_start(800, {-> matchdelete(id, winid)})
else
  call timer_start(800, {-> matchdelete(id)})
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171