tags:

views:

58

answers:

2

In Vim I am trying to paste a few lines:

PROC SQL;
   CONNECT TO DB2(DSN=test);
   CREATE TABLE test AS SELECT *
   FROM CONNECTION TO DB2 (

above every line starting with "select" and

);
quit;

below every line that ends with "FOR FETCH ONLY"

Is there a way to use the paste buffer? Like

%s/^select/(a!)\rselect/

so that it once I type the command it opens a paste buffer like the a! command, and uses that as the substitute?

Thanks, Dan

+2  A: 

If you have the PROC SQL block in register a and the quit; block in register b, then you can simply do the following.

:g/^select/put! a
:g/FOR FETCH ONLY$/put b

:g finds all lines that match the given pattern and then runs the specified ex command on those lines. In this case, you want to use :put to paste the contents of the specified registers.

jamessan
+1  A: 

For your problem of inserting text text above or below the line I would go with jamessan's solution of using :g/.../put a. However to answer your question about using a a register as part of the replacement, you can use an expression for the replacement by starting it with \=. So this should also do what you want, assuming the "PROC SQL;..." text is in register a:

:%s/^select/\=@a."\n".submatch(0)/

in an expression @a evaluates to the contents of register a, and submatch(0) is a function that evaluates to the string matched by the regex.

Note that the entire replacement is treated as an expression, so if you want to include regular text then you need to quote it and concatenate strings with ..

Dave Kirby
I think the `a!` referred to in the original post was the `:append!` command, not the a register.
jamessan