Is it possible to provide some parameter when recording a macro in vim via prompt or some other ways?
Edit: I have such code:
foo
bar
And I would like to surround each with ruby block:
expect do
foo
end.to raise_error(CustomClass)
expect do
foo
end.to raise_error(OtherCustomClass)
So, it is easy to create a macro that will result with: expect do foo end.to raise_error()
expect do
foo
end.to raise_error()
But it will be nice to have prompt that will be used to set raise_error method parameter. In each use of macro this parameter will be different.
To record a macro and save it to a register, type the key q followed by a letter from a to z that represents the register to save the macro, followed by all commands you want to record, and then type the key q again to stop the recording.
Macro parameter values are character sequences of no formal type, and they are comma delimited. There is no way to pass in a comma as part of a parameter value. The number of parameters passed can be less than, greater than, or equal to the number of parameters that the macro value is designed to receive.
Running a macro Suppose you have a macro which operates on the text in a single line. You can run the macro on each line in a visual selection in a single operation: Visually select some lines (for example, type vip to select the current paragraph). Type :normal @q to run the macro from register q on each line.
You start recording by q <letter> and you can end it by typing q again. Recording is a really useful feature of Vim. It records everything you type.
While I agree with everyone else that if you need this feature, you're probably going about things inefficiently, it is possible to insert a variable text string into a document as part of a macro. The trick is to store the text you want to use in your macro in a register.
"xyw
to yank a word into the x registerqq
, when you want to place the variable text, put it, for example "xp
to put the text in the x register into the document where the cursor is@q
, it will use whatever is currently in the x register, so you can yank different text into the x register, play your q macro, and the newly yanked text will be used. If you are talking about recording a macro with qx...q
, this is not possible.
However you could still do : :let @y = substitute(@x, 'old_pattern', 'replacement', 'g')
and then use @y
.
You could also define a function:
function Play(myArg)
execute 'normal sequence_of_characters' . a:myArg . 'other_sequence_of_characters'
endfunction
call Play('foo')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With