aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <rahm@google.com>2025-04-09 03:05:02 +0000
committerJosh Rahm <rahm@google.com>2025-04-09 03:05:02 +0000
commit434fe773449e7bcaa7a233c02f06033188f51b79 (patch)
tree8bf04bb1b869d3381c83fabb6694487cd9ed260f
parent0fa3cc1dbf47935dbab9f1251a8960a1b516d8d6 (diff)
downloadfieldmarshal.vim-434fe773449e7bcaa7a233c02f06033188f51b79.tar.gz
fieldmarshal.vim-434fe773449e7bcaa7a233c02f06033188f51b79.tar.bz2
fieldmarshal.vim-434fe773449e7bcaa7a233c02f06033188f51b79.zip
Add repeat-yank.vim.
This adds a new verb, gy, which behaves exactly like a normal yank command except it's dot-repeatable. When using the dot-repeat, it yank-appends with a newline to the register, so it can be used as an accumulator.
-rw-r--r--plugin/repeat-yank.vim50
1 files changed, 50 insertions, 0 deletions
diff --git a/plugin/repeat-yank.vim b/plugin/repeat-yank.vim
new file mode 100644
index 0000000..50118f7
--- /dev/null
+++ b/plugin/repeat-yank.vim
@@ -0,0 +1,50 @@
+" Makes the 'yank' operator a repeatable operator. When repeating, the yank
+" operator will yank-append into the provided register, seperating each yank
+" entry with a newline.
+"
+" This is ergonomic for collecting snippets (like words) across a file and then
+
+let s:repeat_yank_first=1
+let s:repeat_yank_reg=''
+
+function! s:do_repeatable_yank(typ)
+ let reg = s:repeat_yank_reg
+ let repeat = ! s:repeat_yank_first
+
+ if s:repeat_yank_first
+ let s:repeat_yank_first = 0
+ else
+ let last_reg_info = getreginfo(reg)
+ endif
+
+ if a:typ == 'char'
+ exec 'normal! `[v`]"' . reg . 'y'
+ else
+ exec "'[,']yank " . reg
+ endif
+
+ if repeat
+ let cur_reg_info = getreginfo('"')
+ call setreg(
+ \ reg,
+ \ last_reg_info.regcontents + cur_reg_info.regcontents,
+ \ last_reg_info.regtype )
+ endif
+endfunction
+
+function! s:setup_repeatable_yank() abort
+ let s:repeat_yank_reg = v:register
+ let s:repeat_yank_first = 1
+endfunction
+
+if exists('g:override_native_yank_behavior') && g:override_native_yank_behavior
+ " Option for overriding default yank behavior.
+ nmap yy gy_
+ nmap Y y$
+ nnoremap y <cmd>call <sid>setup_repeatable_yank()<cr><cmd>set operatorfunc<sid>=do_repeatable_yank<cr>g@
+else
+ " By default make a new 'g' command 'gy' and 'gY' for this repeatable yank.
+ nmap gyy gy_
+ nmap gY y$
+ nnoremap gy <cmd>call <sid>setup_repeatable_yank()<cr><cmd>set operatorfunc=<sid>do_repeatable_yank<cr>g@
+endif