I'd like to append some words to a register in Vim. But instead of the words being appended together with no separator, I'd like to add " - " between the words.
I am recording a macro to find the words and append them to a specific register, which is easy to do but I do not know how to tell vim how to append the text separator after each word when I yank the word to the register.
Does anyone have any idea?
qa - start recording macro a
"zyiw - yank a word to register z
For the next step, how do I append " - " to register z before finding and appending the next word?
Thanks.
From Insert mode, pressing <C-r>= drops us into the expression register. We can enter any Vimscript expression. On pressing Enter, Vim evaluates the expression, and inserts the result into the document. For example, if we entered 2*21 into the expression register, Vim would insert 42 into the document.
The append command will put the cursor after the current position, while the insert command will put the cursor before it. Using the append command is like moving the cursor one character to the right, and using the insert command.
Rather than murk around with ~/. viminfo , I tend to "softclear" registers when I'm really and truly done with them by setting them to be blank. To clear the a register, for instance, I type q a q to set the a register to an empty string. Equivalently, :let @a='' does the same.
You will have to do something like:
:let @z .= '-'
before the next "Zyiw
.
This could be automated with a custom mapping:
nnoremap <key> :let @z .= '-' + expand('<cword>')
Reference:
:help :let-@
:help expand()
:help <cword>
Since we are doing vimscript, it is generally recommended to manipulate registers with :help setreg()
and :help getreg()
so we can go with an almost fully programmatic solution:
nnoremap <key> :call setreg('Z', '-' . expand('<cword>'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With