I want to replace each submatch with a string with an incrementing index that starts from 1 in the beginning of each line, so that replacement strings would be varargin{1}
, varargin{2}
, varargin{3}
, etc. For bigger numbers, the number string would naturally need more than one character, e.g.: varargin{9}
, varargin{10}
, etc. The input data is MATLAB code; example inputs and desired outputs are presented below. I'm primarily looking for a Vim solution, but other ways to do this are also appreciated.
The regex below creates running indices beginning from 1, but those change for every line:
:let @a=1 | %s/\v.*'\zs.*\ze\);/\=substitute(submatch(0), '\s[a-zA-Z0-9{}_.]*', ' varargin{'.(@a+setreg('a',@a+1)).'}', 'g')/g
My question is:
How can I reset the index to 1 in the beginning of each line and increment the index by 1 between every submatch?
The code above is a modified version of the "Substitute with ascending numbers" example presented at http://vim.wikia.com/wiki/Substitute_with_incrementing_numbers:
:let @a=1 | %s/abc/\='xyz_'.(@a+setreg('a',@a+1))/g
Example input #1:
messages.msg1.English = xprintf('analysis directory is on %s\n', analysis_dir);
Desired output for example input #1:
messages.msg1.English = xprintf('analysis directory is on %s\n', varargin{1});
Example input #2:
messages.msg15.English = xprintf('the following sessions (%d pcs) have been approved: %s', handling_struct.n_of_accepted, handling_struct.accepted_sessions_vector);
Desired output for example input #2:
messages.msg15.English = xprintf('the following sessions (%d pcs) have been approved: %s', varargin{1}, varargin{2});
Example input #3:
messages.msg19.English = xprintf('looking for files ''%s'' in %d separate dirs', give_file_struct.regex, number_of_dirs);
Desired output for example input #3:
messages.msg19.English = xprintf('looking for files ''%s'' in %d separate dirs', varargin{1}, varargin{2});