Append delimited text to Vim register when appending words

Viewed 77

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.

1 Answers

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>'))
Related