7

Let say I've register which has string stored in it, but I'd like to use substitute command to change it.

For example I've 5x5 html table:

let @t='<table>'.repeat('<tr>'.repeat('<td>_</td>',5).'</tr>',5).'</table>'

and I'd like to replace placeholder (_) with some other data ideally by modifying existing register (not assigning to another).

I've tried the following syntax:

"ts/_/test/g
"@ts/_/test/g " with modifiable set

which doesn't give any errors, but it doesn't work as expected (in similar way as prefixing with % works).

What's the proper way of doing that?

11

Ex commands work on the buffer contents; for register (or variable) contents, you need to use a corresponding Vimscript function (if it exists).

For :substitute, the equivalent is substitute() (that was easy, right?) So:

:let @t = substitute(@t, '_', 'test', 'g')
  • 3
    substitute() isn't completely equivalent to :substitute; the function behaves as if 'magic' is set and 'cpoptions' is empty, whereas the command will observe user-set values. – tommcdo Mar 23 '15 at 22:05
5

Apart from using the let-statement and dealing with vim-script, you need to paste it into a buffer and then yank it back to the register:

  1. "tp
  2. :.s/_/test/g
  3. "tdd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.