Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide parameter in vim macro

Tags:

vim

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.

like image 298
Sławosz Avatar asked Nov 14 '11 10:11

Sławosz


People also ask

How do I create a macro in Vim?

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.

What is parameter in macro?

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.

How do you call a macro in Vim?

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.

What is recording @Q in Vim?

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.


2 Answers

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.

  1. yank some text into a named register, for example "xyw to yank a word into the x register
  2. record your macro, qq, 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
  3. now, when you play your q macro, @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.
like image 172
mkomitee Avatar answered Sep 25 '22 19:09

mkomitee


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')
like image 44
Benoit Avatar answered Sep 21 '22 19:09

Benoit