aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <rahm@google.com>2022-09-12 12:49:36 -0600
committerJosh Rahm <rahm@google.com>2022-09-12 12:49:36 -0600
commitb758ef5504a2ae4f147a5f650993133122d840a7 (patch)
tree154975c175e3aea38eae3d3199b354bbcc8ef896
parentdd9114b3e6874fe198df7b652d1154c09808fe64 (diff)
downloadfieldmarshal.vim-b758ef5504a2ae4f147a5f650993133122d840a7.tar.gz
fieldmarshal.vim-b758ef5504a2ae4f147a5f650993133122d840a7.tar.bz2
fieldmarshal.vim-b758ef5504a2ae4f147a5f650993133122d840a7.zip
put.vim: support for 'cp' operator.
The cp operator replaces a text object with the contents of a register while preserving the value of the default register to improve repeatability.
-rw-r--r--plugin/put.vim36
1 files changed, 36 insertions, 0 deletions
diff --git a/plugin/put.vim b/plugin/put.vim
new file mode 100644
index 0000000..62a0013
--- /dev/null
+++ b/plugin/put.vim
@@ -0,0 +1,36 @@
+" A small plugin to replace a text object with the contents of a register.
+"
+" This is invoked with the 'cp' command. For example
+"
+" cpiw - replace the inner word with the contents of the default register
+" "ncpiw - replace the inner word with the contents of register 'n'
+"
+" Notably, this command does not alter the contents of the default register, so
+" this command may be repeated without worry for changing the editor state.
+
+noremap <silent> cp <cmd>call <sid>setcpreg(v:register)<bar>set operatorfunc=<sid>put<cr>g@
+
+let s:savereg = ''
+let s:cpbuf = '"'
+function! s:setcpreg(r) abort
+ let s:cpbuf = a:r
+endfunction
+
+function! s:put(arg, ...) abort
+
+ if a:0
+ echo a:1
+ let vis = '`<' . a:arg . '`>'
+ elseif a:arg == 'line'
+ let vis = "'[V']"
+ elseif a:arg == 'block'
+ let vis = "`[\<C-v>`]"
+ else
+ let vis = "`[v`]"
+ endif
+
+ let save = getreg('"')
+ silent! exec printf("norm %s\"%sp", vis, s:cpbuf)
+ call setreg('"', save)
+
+endfunction