1
- *usr_52.txt* For Vim version 9.1. Last change: 2024 May 16
1
+ *usr_52.txt* For Vim version 9.1. Last change: 2024 May 31
2
2
3
3
VIM USER MANUAL - by Bram Moolenaar
4
4
@@ -13,6 +13,7 @@ smaller parts.
13
13
| 52.3 | Autoloading without import/export
14
14
| 52.4 | Other mechanisms to use
15
15
| 52.5 | Using a Vim9 script from legacy script
16
+ | 52.6 | Vim9 script examples: comment package, highlight-yank plugin
16
17
17
18
Next chapter: | usr_90.txt | Installing Vim
18
19
Previous chapter: | usr_51.txt | Create a plugin
@@ -336,6 +337,46 @@ will have to make sure to use a unique name for these global items. Example: >
336
337
call g:NicePluginTest()
337
338
338
339
==============================================================================
340
+ *52.6* Vim9 script examples: comment package, highlight-yank plugin
341
+
342
+ COMMENT PACKAGE
343
+
344
+ Vim comes with a comment plugin, written in Vim9 script | comment-install | .
345
+ Have a look at the package located at $VIMRUNTIME/pack/dist/opt/comment/
346
+
347
+ HIGHLIGHT YANK PLUGIN
348
+
349
+ Here is an example for highlighting the yanked region. It makes use of the
350
+ | getregionpos() | function, available since Vim 9.1.0446.
351
+
352
+ Copy the following example into a new file and place it into your plugin directory
353
+ and it will be active next time you start Vim | add-plugin | : >
354
+
355
+ vim9script
356
+
357
+ def HighlightedYank(hlgroup = 'IncSearch', duration = 300, in_visual = true)
358
+ if v:event.operator ==? 'y'
359
+ if !in_visual && visualmode() != null_string
360
+ visualmode(1)
361
+ return
362
+ endif
363
+ var [beg, end] = [getpos("'["), getpos("']")]
364
+ var type = v:event.regtype ?? 'v'
365
+ var pos = getregionpos(beg, end, {type: type})
366
+ var end_offset = (type == 'V' || v:event.inclusive) ? 1 : 0
367
+ var m = matchaddpos(hlgroup, pos->mapnew((_, v) => {
368
+ var col_beg = v[0][2] + v[0][3]
369
+ var col_end = v[1][2] + v[1][3] + end_offset
370
+ return [v[0][1], col_beg, col_end - col_beg]
371
+ }))
372
+ var winid = win_getid()
373
+ timer_start(duration, (_) => m->matchdelete(winid))
374
+ endif
375
+ enddef
376
+
377
+ autocmd TextYankPost * HighlightedYank()
378
+ <
379
+ ==============================================================================
339
380
340
381
Next chapter: | usr_90.txt | Installing Vim
341
382
0 commit comments