I want to copy different strings from different lines of the file and then paste them together at once. In other words, I want to collect different words in a basket and then paste them at once. How can I do this?
Use an uppercase register when yanking (copying):
"Ay
"A
says to append to the a
register, as opposed to "a
which would replace the contents of the a
register.
Once you've copied everything into the register, you can then paste it all at once with:
"ap
-
-
1@John You can clear a register by recording an empty macro to it, like this:
qaq
. Alternatively, if you just want to avoid having previous junk in there, you can copy your first word using the lower case register:"ay
. Remember,a
andA
refer to the same register, they just affect how things are added. – 8bittree Jan 24 '17 at 22:19
You could also define your own custom operator which would yank incrementally any text-object or text covered by a motion:
fu! Incremental_yank(type, ...) abort
if a:type ==# 'char'
norm! `[v`]y
elseif a:type ==# 'line'
norm! '[V']y
elseif a:0
norm! gvy
endif
call setreg('z', @".(a:type ==# 'char' ? ' ' : ''), 'a' . getregtype('"'))
call setreg('"', @z, getregtype('z'))
endfu
nno <silent> zy :<C-U>set opfunc=Incremental_yank<CR>g@
xno <silent> zy :<C-U>call Incremental_yank(visualmode(), 1)<CR>
nno <silent> zyy :<C-U>set opfunc=Incremental_yank<Bar>exe 'norm! '.v:count1.'g@_'<CR>
nno <silent> zyc :<C-U>let [@", @z] = ['', '']<CR>p
In this example, 4 key bindings are installed:
zy
normal operator to incrementally yank a text-object or motionzyy
similar operator which works on lineszy
similar operator which works on visual selectionzyc
normal command to empty the registers"
andz
; useful before beginning to yank a sequence of texts
With it, you could:
- copy some words hitting
zyiw
on the first word, then repeat the operation with the dot command on the next ones - copy some lines hitting
zyy
on the first one, then repeat the operation with the dot command on the next ones - copy some visual selections, hitting
zy
on each of them
The current code clutters the z
register, if you prefer using another register, like x
for example, you could change the following lines:
call setreg('z', @", 'a' . getregtype('"')) call setreg('x', @", 'a' . getregtype('"'))
^ ^
call setreg('"', @z, getregtype('z')) → call setreg('"', @x, getregtype('x'))
^ ^ ^ ^
nno <silent> zyc :<C-U>let [@", @z] = ['', '']<CR>p nno <silent> zyc :<C-U>let [@", @x] = ['', '']<CR>p
^ ^
For more information, see:
:h g@
:h getregtype()
:h setreg()
-
-
-
This is awesome! Could this be changed in order to add a loop to yank-append on multiple visual selections? Say I want to yank all sections no a markdown file. I could
/^#\s.\+
and then dozyv
for instance! – Luciano May 6 '20 at 21:24