Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append delimited text to Vim register when appending words

Tags:

vim

delimiter

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.

like image 966
djme Avatar asked Oct 18 '20 12:10

djme


People also ask

How do I use expression register in Vim?

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.

What is the difference between append and insert in Vim?

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.

How do I delete a register in Vim?

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.


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>'))
like image 146
romainl Avatar answered Oct 20 '22 12:10

romainl