1

so if i have the following multiline text in my register +:

ZERO     
SIGN     
CARRY    
AUXILIARY
PARITY   
OVERFLOW 
INTERRUPT
DIRECTION
TRAP     

And I have the following code:

case ZERO     :
  return a->FLAGS.derp;
case SIGN     :
  return a->FLAGS.derp;
case CARRY    :
  return a->FLAGS.derp;
case AUXILIARY:
  return a->FLAGS.derp;
case PARITY   :
  return a->FLAGS.derp;
case OVERFLOW :
  return a->FLAGS.derp;
case INTERRUPT:
  return a->FLAGS.derp;
case DIRECTION:
  return a->FLAGS.derp;
case TRAP     :
  return a->FLAGS.derp;

I want the result of a search and replace command (i.e. :%s\derp\register+\g) to be

case ZERO     :
  return a->FLAGS.ZERO;
case SIGN     :
  return a->FLAGS.SIGN;
case CARRY    :
  return a->FLAGS.CARRY;
case AUXILIARY:
  return a->FLAGS.AUXILIARY;
case PARITY   :
  return a->FLAGS.PARITY;
case OVERFLOW :
  return a->FLAGS.OVERFLOW;
case INTERRUPT:
  return a->FLAGS.INTERRUPT;
case DIRECTION:
  return a->FLAGS.DIRECTION;
case TRAP     :
  return a->FLAGS.TRAP;

The command :%s/foo/\=@a/g is very close to this but not close enough, it will insert the totality of the register on each match.

1
  • Do you mean where all the cases say "case derp" in your example?
    – Mass
    Commented Oct 15, 2017 at 6:23

1 Answer 1

1

One way to do this is using :h sub-replace-expression with the list flag of getreg(), assuming your code starts at line 1:

%s;derp;\=getreg('+',1,1)[float2nr((line('.')-1)/2)]

If your register looked like this (e.g., using a scratch buffer to duplicate lines):

ZERO     
ZERO     
SIGN     
SIGN     
CARRY  
CARRY   
&c...

this would simplify to

%s;derp;\=getreg('+',1,1)[line('.')]

which is a handy pattern in many situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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